سؤال

associate object tutorial "The tutorial link gave me a clear concept of object-c runtime associate object"

After reading the example code below, I have a small question.

    - (void)setAssociatedObject:(id)object
    {
       objc_setAssociatedObject(self, @selector(associatedObject), object,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

    - (id)associatedObject
    {
       return objc_getAssociatedObject(self, @selector(associatedObject));
    }

If your using ARC in iOS, do we need to add memory handling by ourselves?

Thanks in advance

هل كانت مفيدة؟

المحلول

The call you have to objc_setAssociatedObject will cause the object to be retained — that's the OBJC_ASSOCIATION_RETAIN_NONATOMIC part. That being the specified behaviour, it'll also automatically be released when the object with which it is associated is released.

So you don't need to do any further memory handling whether you're using ARC or not (subject to the caveat that if you wanted this to act like a copy property, you'd obviously need to add copying, which would look different under ARC and non-ARC).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top