Add property
动态的添加属性
#import "NSObject+Objc.h"
#import <objc/runtime.h>
@implementation NSObject (Objc)
-(void)setName:(NSString *)name
{
/* objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
* object :给哪个类添加属性
* key:属性名,根据key,来进行关联,eg:addName来关联name
* objc_AssociationPolicy: 关联策略,就是那种存储类型 NSString用copy撒
*/
objc_setAssociatedObject(self, @"addName", name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(NSString *)name{
return objc_getAssociatedObject(self, @"addName");
}
@end
网友评论