1

I've got to deal with a Dictionary like this, or more nested. How can I access fields like "twotwo" ? Or is there any better possibility to model such structure?

let nestedDict = [
    "fieldOne": "name",
    "fieldTwo": "name",
    "fieldThree":
        [
            [
            "twoOne": "some text",
            "twoTwo": true,
            "twoThree": 1e-40
            ],
            [
            "twoOne": "some text",
            "twoTwo": true,
            "twoThree": 1e-40
            ]
        ]
]
2
  • Have you tried nestedDict["fieldThree"]["twoTwo"]? Commented Oct 21, 2015 at 12:41
  • There are some arrays in between an nestedDict["fieldThree"][0][0]["twoTwo"] will fail Commented Oct 21, 2015 at 12:45

1 Answer 1

3

nestedDict is a Dictionary, you get fieldThree with

let fieldThree = nestedDict["fieldThree"] as! [[String:Any]] // [[String:AnyObject]] in Swift 2 and lower.

fieldThree is an Arrayof [String:AnyObject] dictionaries, you get the value of twoTwo of the first array item with

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

You can even retrieve all values of key twoTwo in the array

let allTwoTwo = fieldThree.map { $0["twoTwo"] as! Bool }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.