2

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

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

在此处输入图像描述

也试过这个:

objectWithReloadFRC.addPointer(UnsafePointer(self)
4

1 回答 1

6

您可以获取指向用于对象的存储的指针 unsafeAddressOf()。由于addPointer()需要一个可变指针,因此需要另一个转换:

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

斯威夫特 3:

var objectWithReloadFRC = NSPointerArray(options: .weakMemory)
objectWithReloadFRC.addPointer(Unmanaged.passUnretained(self).toOpaque())
于 2015-07-02T17:53:43.980 回答