Dictionary 将SwiftUI选择器绑定到字典中保存的值
下面的代码编译并运行,但只有绑定到数组中的值的选择器才能工作。字典里的不及格。你知道为什么会这样吗Dictionary 将SwiftUI选择器绑定到字典中保存的值,dictionary,binding,swiftui,Dictionary,Binding,Swiftui,下面的代码编译并运行,但只有绑定到数组中的值的选择器才能工作。字典里的不及格。你知道为什么会这样吗 import SwiftUI struct ContentView: View { private var options : [String] = ["Bonjour", "Hello", "Hola"] @State private var choicesHeldInArray = [0, 2] @State private var choicesHeldInDi
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])
}
}
}
}
}
导入快捷界面
结构ContentView:View{
私有变量选项:[String]=[“你好”、“你好”、“你好”]
@国家私有变量choicesHeldInArray=[0,2]
@国家私有变量choicesHeldInDict:[字符串:Int]=[“C3”:2,“C4”:1]
var body:一些观点{
VStack{
选择器(选择:$choicesHeldInArray[0],标签:文本(“选择1”)){
ForEach(0..
总之
您不能使用ForEach,因为:
- 它监视一个集合并监视运动。用一本无序的字典是不可能的
- 它监视一个集合并监视运动。用一本无序的字典是不可能的
ForEach
迭代数组选项
。所以我不确定这是不是原因。这里的问题是存储选项的位置。在上述每种情况下,ForEach
都会迭代数组选项
。所以我不确定这是不是原因。这里的问题是选择存储在哪里。