我在想,这个问题可能很容易解决,但到目前为止我还没有找到解决方案.
我试图显示一个字典,并允许用户 Select 一个项目,这样我就可以自动访问他们 Select 的键和值
在列出词典时,我能够同时显示键和值--例如
Text("Key:\t\t\(item)\t\t\t")
Text("Value:\t\(myList[item] ?? "")”)
但当用户做出 Select 时,我只能访问并打印密钥,而不能通过文本视图显示
print("Selection is:\($selection)")
不是:
Text("Selection is:\(selection)")
或
Text("Selection is:\($selection)")
我认为这可能与我将 Select 变量设置为:
@State private var selection = Set<String>()
我try 将其配置为:
@State private var selection = Set<String:String>()
but get err或 meessages f或 this.
如有任何建议,我们将不胜感激.
完整代码为:
imp或t SwiftUI
struct ContentView: View {
var myList = ["Chumbawumba": "7", "Jini Dosa": "2", "Handbrake": "13"]
@State private var selection = Set<String>()
var body: some View {
List(selection: $selection) {
F或Each(myList.keys.s或ted(), id: \.self) { item in
HStack {
Text("Key:\t\t\(item)\t\t\t")
Text("Value:\t\(myList[item] ?? "")")
Spacer()
}
}
}
// Text("Selection is:")
// Text(selection)
// Text("Selection is:\(selection)")
Button(action: {
print("Selection is:\(selection)")
}) {
Text("Get")
.padding()
}
.padding()
}
}