标签:
MJExtension
,JSONModel
关联对象
为分类添加属性交换方法
拦截系统或其他框架的方法误区:并不是使用的技术越底层,框架的效率就会越高
代码演练
#import <objc/runtime.h>
+ (NSArray *)properties {
unsigned int count = 0;
class_copyPropertyList(self.class, &count);
NSLog(@"属性数量 %u", count);
return @[@"title", @"digest", @"replyCount", @"imgsrc"];
}
+ (NSArray *)properties {
unsigned int count = 0;
objc_property_t *list = class_copyPropertyList(self.class, &count);
for (unsigned int i = 0; i < count; ++i) {
const char *cname = property_getName(list[i]);
printf("%s\t", cname);
}
printf("\n");
// 释放属性数组
free(list);
return @[@"title", @"digest", @"replyCount", @"imgsrc"];
}
+ (NSArray *)properties {
unsigned int count = 0;
objc_property_t *list = class_copyPropertyList(self.class, &count);
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:count];
for (unsigned int i = 0; i < count; ++i) {
const char *cname = property_getName(list[i]);
[arrayM addObject:[NSString stringWithUTF8String:cname]];
}
// 释放属性数组
free(list);
NSLog(@"%@", arrayM);
return arrayM;
}
/// 关联对象键值
const char* propertiesKey = "propertiesKey";
...
// 判断是否能存在关联对象
NSMutableArray *arrayM = objc_getAssociatedObject(self, propertiesKey);
if (arrayM != nil) {
return arrayM;
}
...
// 设置关联对象
objc_setAssociatedObject(self, propertiesKey, arrayM, OBJC_ASSOCIATION_COPY_NONATOMIC);
标签:
原文地址:http://www.cnblogs.com/zl-wlt/p/4915252.html