给category增加属性

时间:2022-02-12 16:26:12
//分类的头文件 @interface ClassName (CategoryName) @property (nonatomic, strong) NSString *str; @end //实现文件 #import "ClassName + CategoryName.h" #import <objc/runtime.h>
static void *strKey = &strKey;
@implementation ClassName (CategoryName) -(void)setStr:(NSString *)str      objc_setAssociatedObject(self, & strKey, str, OBJC_ASSOCIATION_COPY); 
-(NSString *)str      return objc_getAssociatedObject(self, &strKey);  } @end
给category增加属性
给category增加属性

这里要注意objc_setAssociatedObject里传的参数,枚举值等等有的时候是copy有的时候是strong或者retain