Normally when using objc_setAssociatedObject
, the recommended practice is to create a static variable, then use its address (via the '&' prefix) as the key.
However, we have a case where we need to associate an arbitrary number of classes with one 'owner' class, and as such, each needs its own unique key. My thought was to use the actual address of the class instance for that purpose.
The issue I'm running into is I can only seem to get the address as an Int, but the function expects an UnsafeRawPointer
and I haven't found any code that lets me initialize, or convert to one from an int.
So again, while I can get the address as an int with this code...
func addressOf<T: AnyObject>(_ object: T) -> Int {
return unsafeBitCast(object, to:Int.self)
}
let classA = MyClass()
let classB = classA
let a = addressOf(classA)
let b = addressOf(classB)
// Result: a == b
I can't get a/b as an UnsafeRawPointer
. Is this even possible?