

编译的时候会有警告,不,我的直接崩了。。。
因为分类里面写的属性 不会生成 实例变量 并且不会生成set get 方法
如果想在分类里面实现这个功能 可以通过关联
#import "Person+Friendship.h"
#importstatic const char * kFriendsPropertyKey = "kFriendsPropertyKey";
@implementation Person (Friendship)
- (NSArray *)friends
{
return objc_getAssociatedObject(self,kFriendsPropertyKey);
}
- (void)setFriends:(NSArray *)friends
{
objc_setAssociatedObject(self, kFriendsPropertyKey, friends, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
网友评论