我想将弱引用存储在NSPointerArray中,但出现错误:

public var objectWithReloadFRC = NSPointerArray(options: NSPointerFunctionsWeakMemory)
objectWithReloadFRC.addPointer(self) //self is an UIViewController subclass

也尝试了这个:
objectWithReloadFRC.addPointer(UnsafePointer(self)

最佳答案

您可以使用以下方法获取指向用于对象的存储的指针:unsafeAddressOf()。由于addPointer()需要可变的指针,
需要另一次转换:

objectWithReloadFRC.addPointer(UnsafeMutablePointer(unsafeAddressOf(self)))

Swift 3:
var objectWithReloadFRC = NSPointerArray(options: .weakMemory)
objectWithReloadFRC.addPointer(Unmanaged.passUnretained(self).toOpaque())

关于ios - 如何将对象添加到NSPointerArray?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31191067/

10-11 05:35