【发布时间】:2014-08-24 15:11:27
【问题描述】:
我正在尝试在字典中插入新的键值对,它嵌套在另一个 Dictionary:
var dict = Dictionary<Int, Dictionary<Int, String>>()
dict.updateValue([1 : "one", 2: "two"], forKey: 1)
dict[1]?[1] // {Some "one"}
if var insideDic = dict[1] {
// it is a copy, so I can't insert pair this way:
insideDic[3] = "three"
}
dict // still [1: [1: "one", 2: "two"]]
dict[1]?[3] = "three" // Cannot assign to the result of this expression
dict[1]?.updateValue("three", forKey: 3) // Could not find a member "updateValue"
我相信应该是一个简单的方法来处理它,但我花了一个小时仍然无法弄清楚。
我可以改用NSDictionary,但我真的很想了解我应该如何在 Swift 中管理嵌套的Dictionaries?
【问题讨论】:
-
是的,本质上是一样的斗争。对不起,我第一次错过了。我应该以某种方式链接到它并关闭它吗?
标签: dictionary collections swift