我正在尝试在 SwiftUI 中使用 ForEach 将简单的字符串数组传递到列表中。
这是我的代码
import SwiftUI struct ContentView : View { var testArry:[String] = ["1", "a", "c"] var body: some View { List { ForEach(testArry) { obj in Text("test") } } } } #if DEBUG struct ContentView_Previews : PreviewProvider { static var previews: some View { ContentView() } } #endif
Error : Unable to infer closure type in the current context
最佳答案
或者你可以像这样使用 .identified(by:\.self)
:
import SwiftUI
struct ContentView : View {
var testArry:[String] = ["1", "a", "c"]
var body: some View {
List {
ForEach(testArry.identified(by: \.self)) { obj in
Text("test")
}
}
}
}
关于ios - 无法在 SwiftUI 的当前上下文中推断闭包类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56835133/