gpt4 book ai didi

dictionary - 将 SwiftUI Picker 绑定(bind)到字典中保存的值

转载 作者:行者123 更新时间:2023-12-05 07:11:14 26 4
gpt4 key购买 nike

以下代码编译并运行,但只有绑定(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])
}
}
}
}
}

最佳答案

Could the answer be something because of this?

简而言之您不能使用 ForEach 因为:

  • 它会观察集合并监控运动。使用无序字典是不可能的。

关于dictionary - 将 SwiftUI Picker 绑定(bind)到字典中保存的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60853197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com