Dictionary or Tuple? How to access a key inside another key in Swift?

1

I have a web service in which, with the Alamo library, I collect some data. I have the result in a variable such as: response.result.value Within this variable I want to access a second level key.

The result is returned as:

response.result.value as! NSDictionary

I've also tried like:

response.result.value as! [String:Any]

In another function I pick it up and access the first key as:

result?["key1"]

Until there is everything right, now I try to access another key that is in the next level:

result?["key1"]["key2"]

Here I already throws an error.

How can I access the next level?

    
asked by Popularfan 02.08.2017 в 17:41
source

1 answer

3

You can assign the first level to a variable, for example:

let abc = [
"key1": 1,
"key2": [
    "a": 22,
    "b": 33
  ]
] as [String : Any]

print(abc["key1"]) // 1

let res = abc["key2"] as! [String: Any]

print(res["a"]) // 22
    
answered by 03.08.2017 / 00:54
source