Dor Cohen
Dor Cohen

Reputation: 17080

Access a dictionary in dictionary

I have a dictionary which contains another dictionary:

var channelDict = Dictionary<String, AnyObject>()
channelDict["image"] = Dictionary<String, AnyObject>()

Now I want to add value to the child dictionary, I tried the following but nothing worked:

channelDict["image"]["key"] = "value"

var:Dictionary<String, AnyObject> dict = channelDict["image"]
dict["key"] = "value"

if var dict = channelDict["image"] {
       dict["Key"] = "value"
}

Upvotes: 2

Views: 421

Answers (1)

D&#225;niel Nagy
D&#225;niel Nagy

Reputation: 12015

This way you can add a dictionary to the "image" key:

channelDict["image"] = ["key": "value"]

UPDATED

And if you want to add values to the inner dict and keep the existing values, you should create a temporary variable like this:

var innerDict = channelDict["image"] as [String: AnyObject]

innerDict["dsa"] = "dsa"

channelDict["image"] = innerDict

Upvotes: 2

Related Questions