当我从字典中获取值并将它们放入数组时,我无法再释放内存。我试图从数组和字典中删除所有对象,但这些对象仍然存在于某处(未调用 deinit)。
我是这样玩的:
class MyData {
let i = 0
init () {
NSLog("Init")
}
deinit {
NSLog("Deinit")
}
}
var myDictionary:Dictionary<String, MyData> = ["key1":MyData(), "key2":MyData()]
// Init was called twice
// here is the problem: extract values from Dictionary
var myValues = Array(myDictionary.values)
myValues = [] // nothing - ok, strong references are still in the dictionary
myDictionary = [:]
// Why Deinit was not called???
如果我删除这两行进行值提取,则正常调用 Deinit
var anotherDictionary:Dictionary<String, MyData> = ["key1":MyData(), "key2":MyData()]
anotherDictionary = [:]
// Deinit is called twice
var myArray:MyData[] = [MyData(), MyData()]
myArray = []
// Deinit is called twice
我在这里做错了什么?
当不再需要对象时,如何以正确的方式删除对象以释放内存?仅当从 Dictionary(Dictionary.values 或 Dictionary.keys)中提取键或值时才会出现此问题。
编辑:
我为这种情况做了一个解决方法。如果我使用 NSDictionary 而不是 Dictionary 并首先提取键,然后在 for 循环中取值,那么它就可以工作。
var myDictionary:NSDictionary = ["key1":MyData(), "key2":MyData()]
// called Init twice
var myKeys:String[] = myDictionary.allKeys as String[]
var myValues:MyData[] = []
for key in myKeys {
myValues.append(myDictionary[key] as MyData)
}
myKeys = []
myDictionary = [:]
myValues = []
// Deinit is now called twice, but I'm not sure about keys...
但是,如果我使用 allValues 而不是 allKeys,那么它将不再起作用。
...
var anotherValues = myDictionary.allValues
anotherValues = []
myDictionary = [:]
// deinit is not called again - very scary...