我正在使用Swift 2,并使用WeakContainer作为存储一组弱对象的方法,就像 NSHashTable.weakObjectsHashTable()
NSHashTable.weakObjectsHashTable()
struct WeakContainer<T: AnyObject> { weak var value: T? } public protocol MyDelegate : AnyObject { }
然后在我的ViewController中,声明
public var delegates = [WeakContainer<MyDelegate>]
但这是错误的
不支持将MyDelegate用作符合协议AnyObject的具体类型
我看到错误是成员WeakContainer已value声明为weak,因此T应该是对象。但是我也声明MyDelegate为AnyObject。如何解决这个问题?
WeakContainer
value
weak
T
MyDelegate
AnyObject
我有相同的想法,用泛型创建弱容器。 结果,我为创建了包装器,NSHashTable并为您的编译器错误做了一些解决方法。
NSHashTable
class WeakSet<ObjectType>: SequenceType { var count: Int { return weakStorage.count } private let weakStorage = NSHashTable.weakObjectsHashTable() func addObject(object: ObjectType) { guard object is AnyObject else { fatalError("Object (\(object)) should be subclass of AnyObject") } weakStorage.addObject(object as? AnyObject) } func removeObject(object: ObjectType) { guard object is AnyObject else { fatalError("Object (\(object)) should be subclass of AnyObject") } weakStorage.removeObject(object as? AnyObject) } func removeAllObjects() { weakStorage.removeAllObjects() } func containsObject(object: ObjectType) -> Bool { guard object is AnyObject else { fatalError("Object (\(object)) should be subclass of AnyObject") } return weakStorage.containsObject(object as? AnyObject) } func generate() -> AnyGenerator<ObjectType> { let enumerator = weakStorage.objectEnumerator() return anyGenerator { return enumerator.nextObject() as! ObjectType? } } }
用法:
protocol MyDelegate : AnyObject { func doWork() } class MyClass: AnyObject, MyDelegate { fun doWork() { // Do delegated work. } } var delegates = WeakSet<MyDelegate>() delegates.addObject(MyClass()) for delegate in delegates { delegate.doWork() }
这不是最佳解决方案,因为WeakSet可以使用任何类型进行初始化,并且如果此类型不符合AnyObject协议,则应用程序将崩溃。但是我现在没有更好的解决方案。
WeakSet