【发布时间】:2015-10-10 22:21:50
【问题描述】:
简而言之,
我创建了一个由整数和字符串数组组成的对象。
我在整数和数组中都放了一些东西
我将对象放入字典
我获得了对该对象的新引用并尝试追加到数组中。这就是失败的地方
//: Playground - noun: a place where people can play
class IdAndArray {
var id: Int = 0
var stringArray:[String] = [String]()
}
var dict:[Int:IdAndArray] = [Int:IdAndArray]()
var testRec = IdAndArray()
testRec.id = 1
testRec.stringArray.append("1 line")
dict[56] = testRec
var ref = (dict[56]?.stringArray)!
print(ref.count) // 1
ref.append("2 line")
var anotherRef = (dict[56]?.stringArray)!
print(anotherRef.count) // 1 instead of 2???
我怀疑这可能与此有关: Implementing a multimap in Swift with Arrays and Dictionaries
我也怀疑这与选项有关,但我用 ! 强制展开
为了记录,我来自 java 背景。
【问题讨论】:
标签: arrays swift dictionary