SwiftUI 中带有循环的动态按钮

Saj*_*jad 2 ios swift swiftui xcode11

我正在使用SwiftUIXcode-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 可以编译它

bac*_*h-f 6

要打印IntText("\(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)

结果