/********关联机制*********/
/**
关联机制的应用
id object 要被关联的对象
const void *key 要关联的KEY
id value 要关联的值
objc_AssociationPolicy policy 关联策略(内存机制)
*/
#import <objc/runtime.h>
const void *webCacheKEY = @"web";
@property (copy, nonatomic) NSString *currentAddr;
****************华丽的分割线********************
-(void)setCurrentAddr:(NSString *)currentAddr{
objc_setAssociatedObject(self, webCacheKEY, currentAddr, OBJC_ASSOCIATION_COPY);
}
-(NSString *)currentAddr{
return objc_getAssociatedObject(self, webCacheKEY);
}
/*********动态获取模型中的属性值********/
#import <objc/runtime.h>
- (NSArray *) getProject {
//class_copyPropertyList 拷贝类的属性列表
unsigned int count;
objc_property_t *project = class_copyPropertyList(self.class, &count);
NSMutableArray *array = [NSMutableArray array];
//从属性列表取出每一个属性
for (int i = 0; i < count; i++) {
objc_property_t pro = project[i];
//c --> oc
const char *name = property_getName(pro);
//c的名称转换为oc的名称
NSString *property = [[NSString alloc] initWithUTF8String:name];
[array addObject:property];
}
return [array copy];
}
转载于:https://my.oschina.net/venn0126/blog/541125