1

关联对象教程 “教程链接让我对object-c运行时关联对象有了一个清晰的概念”

阅读下面的示例代码后,我有一个小问题。

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

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

如果您在 iOS 中使用 ARC,我们是否需要自己添加内存处理?

提前致谢

4

1 回答 1

3

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).

于 2014-04-22T22:16:48.317 回答