2015-10-21 79 views
0

我必須處理像這樣的字典,或者更多的嵌套字典。 如何訪問「twotwo」等字段?還是有更好的可能性來建模這樣的結構?嵌套字典中的快速訪問字段

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

你試過'nestedDict [「fieldThree」] [「twoTwo」]'? – Arc676

+0

有一些數組在nestedDict [「fieldThree」] [0] [0] [「twoTwo」]將失敗 – SnowMax

回答

1

nestedDictDictionary,你fieldThree

let fieldThree = nestedDict["fieldThree"] as! [[String:AnyObject]] 

fieldThree[String:AnyObject]Array的字典,你得到的第一個數組元素的twoTwo值與

let twoTwo = fieldThree[0]["twoTwo"] as! Bool 

您甚至可以檢索關鍵字的所有值0在陣列中

let allTwoTwo = fieldThree.map { $0["twoTwo"] as! Bool } 
+0

非常感謝! :) –