如何在Swift中设置弱引用

Ham*_*mer 7 foundation swift xcode6

NSSet拥有对其对象的强引用,因此Objective C解决方案是使用NSHashTable.weakObjectsHashTable()a la

NSHashTable* mySet = [NSHashTable weakObjectsHashTable];
[mySet addObject:anyOldObject];
[mySet count]; //returns 1
[mySet containsObject:anyOldObject]; //returns true
Run Code Online (Sandbox Code Playgroud)

但是,这似乎没有用

var mySet = NSHashTable.weakObjectsHashTable()
mySet.addObject(anyOldObject)
mySet.count //returns 1
mySet.containsObject(anyOldObject) //returns false
Run Code Online (Sandbox Code Playgroud)

我错过了什么?或者这是一个错误?

AlB*_*lue 1

在 Xcode6b5 上并使用字符串作为 anyOldObject,我可以看到 return true 发生。6b4 中存在一些错误,导致可选值无法在解释器中返回,这意味着它们正在被积极清理 - 可能是这样的问题,在这里具有相同的效果。值得重新尝试看看您在最新的测试版中是否有相同的行为。