在字典中访问字典 [英] 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"
}
推荐答案
您可以添加一个字典到image
键:
This way you can add a dictionary to the "image"
key:
channelDict["image"] = ["key": "value"]
UPDATED
如果要向内部dict添加值并保留现有值,则应创建一个临时变量:
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
这篇关于在字典中访问字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文