János
János

Reputation: 35112

How to add object to an NSPointerArray?

I want to store weak references in an NSPointerArray but I get an error:

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

enter image description here

tried this too:

objectWithReloadFRC.addPointer(UnsafePointer(self)

Upvotes: 2

Views: 3461

Answers (1)

Martin R
Martin R

Reputation: 540055

You can get a pointer to the storage used for an object with unsafeAddressOf(). Since addPointer() requires a mutable pointer, another conversion is needed:

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

Swift 3:

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

Upvotes: 6

Related Questions