笔记04 category
2021-03-12 本文已影响0人
PPFSaber
四 category


越靠后编译的分类方法插入的越靠前,即优先级越高,编译顺序在 compile source 里




分类的属性
1 声明 分类的属性系统只会帮你声明 set和get 方法,不会实现这两个方法,需要你自己去实现
@property (copy, nonatomic) NSString* name;
关联对象

另一种方式
#define MJKey [NSString stringWithFormat:@"%p", self]
@implementation MJPerson (Test)
NSMutableDictionary *names_;
NSMutableDictionary *weights_;
+ (void)load
{
weights_ = [NSMutableDictionary dictionary];
names_ = [NSMutableDictionary dictionary];
}
- (void)setName:(NSString *)name
{
// NSString *key = [NSString stringWithFormat:@"%p", self];
names_[MJKey] = name;
}
- (NSString *)name
{
// NSString *key = [NSString stringWithFormat:@"%p", self];
return names_[MJKey];
}
- (void)setWeight:(int)weight
{
// NSString *key = [NSString stringWithFormat:@"%p", self];
weights_[MJKey] = @(weight);
}
- (int)weight
{
// NSString *key = [NSString stringWithFormat:@"%p", self];
return [weights_[MJKey] intValue];
}
@end
