首页 > 其他 > 详细

Category和Associative

时间:2014-12-25 20:35:44      阅读:246      评论:0      收藏:0      [点我收藏+]

Category,类目,可以为已有的类添加新的方法,但是要想在Category里扩展属性就要使用Runtime的Associative,使用Runtime来添加类目的属性。


static void * MyObjectMyCustomPorpertyKey = (void *)@"MyObjectMyCustomPorpertyKey";

@implementation MyObject (ExtendedProperties)

- (id)myCustomProperty
{
        return objc_getAssociatedObject(self, MyObjectMyCustomPorpertyKey);
}

- (void)setMyCustomProperty:(id)myCustomProperty
{
        objc_setAssociatedObject(self, MyObjectMyCustomPorpertyKey, myCustomProperty, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end


以上即是使用Associative把self和myCustomProperty两个对象进行关联,从而达到为类目添加属性的目的。


objc_setAssociatedObject(self, KEY_TAGSTRING, nil,OBJC_ASSOCIATION_RETAIN_NONATOMIC);   //可以用来断开Associative



Category和Associative

原文:http://blog.csdn.net/superleexpert/article/details/42150205

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!