Ios 在SwiftUI中为数据字典发出读取值

Ios 在SwiftUI中为数据字典发出读取值,ios,swift,swiftui,Ios,Swift,Swiftui,我有以下字典: let selection: [String: [String : Bool]] 我可以这样显示数据: ForEach(selection.keys.sorted(), id: \.self) { item in Text("\(item.description)") .font(.caption) .fontWeight(.bold) } 上面正确打印了字典的键 我遇到的问题是如何访问字典的值(或剩余部分[S

我有以下字典:

let selection: [String: [String : Bool]]
我可以这样显示数据:

ForEach(selection.keys.sorted(), id: \.self) {  item in

    Text("\(item.description)")
        .font(.caption)
        .fontWeight(.bold)

}
上面正确打印了字典的键


我遇到的问题是如何访问字典的值(或剩余部分[String:Bool])?

您选择的方法可能取决于您的意图。以下是访问
[String:Bool]
数据的几种方法。这个例子很做作,因为它是一堆不同的方法粘在一起的,但是它应该给你一些关于访问数据的方法的想法

struct ContentView : View {
    
    let selection: [String: [String : Bool]] = [:]

    var body: some View {
        ForEach(selection.keys.sorted(), id: \.self) {  key in
            Text("\(key)")
                .font(.caption)
                .fontWeight(.bold)
            
            let item = selection[key]! //get the [String: Bool] dictionary item
            ForEach(item.keys.sorted(), id: \.self) { secondaryKey in
                Text("\(secondaryKey)")
                Text("\((item[secondaryKey] ?? false) ? "true" : "false")")
            }
        }
        
        ForEach(selection.map { ($0,$1) }, id: \.0) { (key, value) in
            Text(key)
            ForEach(value.keys.sorted(), id: \.self) { secondaryKey in
                Text("\(secondaryKey)")
            }
            Text("MyKey = \((value["myKey"] ?? false) ? "true" : "false")")
        }
    }
}

不相关,但对字符串调用
description
是多余的。只需写下
文本(项目)
和名称
项目
就可以了。谢谢!!!!你是一个救生员!!!