Saj*_*jad 2 ios swift swiftui xcode11
我正在使用SwiftUI与Xcode-beta 4.
我想创建动态按钮ForEach。我正在使用这些代码行:
struct Result {
var id = UUID()
var score: Int
}
struct ContentView : View {
let results = [Result(score: 8), Result(score: 5), Result(score: 10)]
var body: some View {
VStack {
ForEach(results.identified(by: \.id)) { result in
Button(action: {
print(result.score)
}){
Text(result.score)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但Xcode无法编译这个。
当我更改Text(result.score)为Text("test")按钮样式时,现在 Xcode 可以编译它
要打印Int:Text("\(result.score)")
另请记住,ForEach语法发生了一点变化(Beta 5)。
现在应该是:results.identified(by: \.id)
let results = [Result(score: 8), Result(score: 5), Result(score: 10)]
var body: some View {
VStack {
ForEach(results.identified(by: \.id)) { result in
Button(action: {
print(result.score)
}){
Text("\(result.score)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5307 次 |
| 最近记录: |