标签:
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
标签:
原文地址:http://blog.csdn.net/superleexpert/article/details/42150205