OC对象给分类加入属性

OC对象中不能给分类加入属性。可是在实际开发中。常常为了更好的性能须要给分类加入属性,那么

加入的属性不能有默认的成员变量。须要我们自己实现set和get方法,要用到执行时

例如以下:

#import <objc/runtime.h>


//执行时的关联对象。动态加入属性

const void *URLStringKey = "URLStringKey";

//set方法

- (void)setUrlStr:(NSString *)urlStr

{

    objc_setAssociatedObject(self, URLStringKey, urlStr, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

//get方法

- (NSString *)urlStr

{

    return objc_getAssociatedObject(self, URLStringKey);

}



原文地址:https://www.cnblogs.com/blfbuaa/p/7270301.html