以下代码编译并运行,但只有绑定(bind)到数组中的值的选择器才能工作。那些在字典里的人失败了。知道为什么会这样吗?
import SwiftUI
struct ContentView: View {
private var options : [String] = ["Bonjour", "Hello", "Hola"]
@State private var choicesHeldInArray = [0, 2]
@State private var choicesHeldInDict: [String: Int] = ["C3": 2, "C4":1]
var body: some View {
VStack {
Picker(selection: $choicesHeldInArray[0], label: Text("Choice 1")) {
ForEach(0 ..< options.count) {
Text(self.options[$0])
}
}
Picker(selection: $choicesHeldInArray[1], label: Text("Choice 2")) {
ForEach(0 ..< options.count) {
Text(self.options[$0])
}
}
Picker(selection: $choicesHeldInDict["C3"], label: Text("Choice 3")) {
ForEach(0 ..< options.count) {
Text(self.options[$0])
}
}
Picker(selection: $choicesHeldInDict["C4"], label: Text("Choice 4")) {
ForEach(0 ..< options.count) {
Text(self.options[$0])
}
}
}
}
}
最佳答案
关于dictionary - 将 SwiftUI Picker 绑定(bind)到字典中保存的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60853197/