我必须处理像这样的词典,或者更多嵌套的词典。
如何访问“twotwo”之类的字段?还是有更好的可能性为这种结构建模?

let nestedDict = [
    "fieldOne": "name",
    "fieldTwo": "name",
    "fieldThree":
        [
            [
            "twoOne": "some text",
            "twoTwo": true,
            "twoThree": 1e-40
            ],
            [
            "twoOne": "some text",
            "twoTwo": true,
            "twoThree": 1e-40
            ]
        ]
]

最佳答案

nestedDictDictionary,您可以通过以下方式获得fieldThree

let fieldThree = nestedDict["fieldThree"] as! [[String:Any]] // [[String:AnyObject]] in Swift 2 and lower.
fieldThreeArray字典的[String:AnyObject],您可以使用以下命令获取第一个数组项的twoTwo的值
let twoTwo = fieldThree[0]["twoTwo"] as! Bool

您甚至可以检索数组中twoTwo键的所有值
let allTwoTwo = fieldThree.map { $0["twoTwo"] as! Bool }

10-08 05:50