我必须处理像这样或更多嵌套的字典。 我怎样才能访问像“twotwo”这样的字段?或者是否有更好的可能性来模拟这种结构?
let nestedDict = [
"fieldOne": "name",
"fieldTwo": "name",
"fieldThree":
[
[
"twoOne": "some text",
"twoTwo": true,
"twoThree": 1e-40
],
[
"twoOne": "some text",
"twoTwo": true,
"twoThree": 1e-40
]
]
]
最佳答案
nestedDict
是一个字典
,你得到fieldThree
let fieldThree = nestedDict["fieldThree"] as! [[String:Any]] // [[String:AnyObject]] in Swift 2 and lower.
fieldThree
是 [String:AnyObject]
字典的Array
,你得到 twoTwo
的值带有
let twoTwo = fieldThree[0]["twoTwo"] as! Bool
您甚至可以检索数组中键 twoTwo
的所有值
let allTwoTwo = fieldThree.map { $0["twoTwo"] as! Bool }
关于ios - 嵌套字典中的 Swift 访问字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33259519/