我现在使用 SwiftUI/Swift 一个星期了,我喜欢它。现在我有一个问题。我想从我的视图中调用一个函数,但我收到这个错误
类型 '()' 不能符合 'View';只有结构/枚举/类类型可以符合协议
这是代码:
struct testView: View {
var body: some View {
VStack {
Text("TextBox")
Text("SecondTextBox")
self.testFunction()
}
}
func testFunction() {
print("This is a Text.")
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白。在其他语言中,它要简单得多,并且可以那样工作。有人可以帮我吗?Swift 对我来说很新:D
Asp*_*eri 14
同时这里是你可以调用函数的地方(不是全部)
init() {
self.testFunction() // non always good, but can
}
var body: some View {
self.testFunction() // 1)
return VStack {
Text("TextBox")
.onTapGesture {
self.testFunction() // 2)
}
Text("SecondTextBox")
}
.onAppear {
self.testFunction() // 3)
}
.onDisappear {
self.testFunction() // 4)
}
}
Run Code Online (Sandbox Code Playgroud)
... 等等
| 归档时间: |
|
| 查看次数: |
4748 次 |
| 最近记录: |