準(zhǔn)備工作 導(dǎo)入需要的頭文件
#import <objc/runtime.h>
創(chuàng)建分類并聲明一個(gè)屬性
#import "Persion.h"
@interface Persion (mult)
@property (nonatomic,copy) NSString* nick;
@end
分類中屬性實(shí)現(xiàn)
const char* name = "rylsj";
- (void) setNick:(NSString *)nick {
objc_setAssociatedObject(self, &name, nick, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString*) nick {
return objc_getAssociatedObject(self, &name);
}
代碼調(diào)用
- (void) rylsj_Test {
self.persion = [[Persion alloc] init];
self.persion.nick = @"RunyaLsj";
NSLog(@"%@", self.persion.nick);
}