准备工作 导入需要的头文件
#import <objc/runtime.h>
创建分类并声明一个属性
#import "Persion.h"
@interface Persion (mult)
@property (nonatomic,copy) NSString* nick;
@end
分类中属性实现
const char* name = "rylsj";
- (void) setNick:(NSString *)nick {
objc_setAssociatedObject(self, &name, nick, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString*) nick {
return objc_getAssociatedObject(self, &name);
}
代码调用
- (void) rylsj_Test {
self.persion = [[Persion alloc] init];
self.persion.nick = @"RunyaLsj";
NSLog(@"%@", self.persion.nick);
}