category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性

@interface NSObject (CategoryWithProperty)

@property (nonatomic, strong) NSObject *property;

@end

@implementation NSObject (CategoryWithProperty)

- (NSObject *)property {

return objc_getAssociatedObject(self, @selector(property));

}

- (void)setProperty:(NSObject *)value {

objc_setAssociatedObject(self, @selector(property), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

@end

你可能感兴趣的:(category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性)