It appears that objc_setAssociatedObject causes objects to be released early.
I followed the method mentioned here to set the association.
import ObjectiveC
// Define a variable whose address we'll use as key.
// "let" doesn't work here.
var kSomeKey = "s"
…
func someFunc() {
var value = MyOtherClass()
objc_setAssociatedObject(target, &kSomeKey, value, UInt(OBJC_ASSOCIATION_RETAIN))
let assocValue : AnyObject! = objc_getAssociatedObject(target, &kSomeKey)
}
This results in the object being released during the objc_setAssociatedObject call. As you can see by this stacktrace.
[MyOtherClass dealloc]
_objc_object::sidetable_release(bool)
_object_set_associative_reference
MyApp.MyClass.someFunc()
I originally thought it might have had something to do with swift classes so I also tried standard Objective-C classes and deinit or dealloc are called during the objc_setAssociatedObject call.
Further adding to my confusion is that objc_getAssociatedObject appears to return a valid object and I can access it variables without error.
Is this a swift bug or have I used objc_setAssociatedObject incorrectly?
I am using Xcode6 beta5 in case that is relevant.