I am trying to associated a property to an Array Extension:
private var AssociatedObjectHandle: String = "BlaBLabla"
extension Array {
var emptyIndex:Int {
mutating get {
if let object = objc_getAssociatedObject(self, &AssociatedObjectHandle) {
return object as! Int
}
let index = self.searchEmptyIndex()
self.emptyIndex = index
return index
}
set {
let new = (newValue as NSInteger)
objc_setAssociatedObject(self, &AssociatedObjectHandle, new, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
func searchEmptyIndex() -> Int {
if let arr = self as? [Int] {
return arr.index(of: -1)!
}
return -1
}
}
The objc_getAssociatedObject call always returns nil!!
Anyone has any idea why?
I am banging my head for the last hour about this...