runtime-实现字典转模型
模型属性,通常需要跟字典的key一一对应
问题:一个一个的生成模型属性,很慢
需求:能不能自动根据一个字典,生成对应的属性
解决:提供一个分类,专门根据字典生成对应的属性字符串
#import "NSObject+Model.h"
#import <objc/runtime.h>
const char *key = "key";
@implementation NSObject (Model)
+ (instancetype)modelWithDic:(NSDictionary *)dic
{
// 实例化当前对象
id objc = [[self alloc] init];
// 获取self 的属性列表
NSArray *proArr = [self getPropertyArr];
// 遍历字典
[dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSString *type;
if ([obj isKindOfClass:NSClassFromString(@"__NSCFString")]) {
type = @"NSString";
}else if ([obj isKindOfClass:NSClassFromString(@"__NSCFArray")]){
type = @"NSArray";
}else if ([obj isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
type = @"int";
}else if ([obj isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
type = @"NSDictionary";
}else if ([obj isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
type = @"BooL";
}
NSLog(@"==========%@",type);
// 判断 属性列表中是否包含这个 key
if ([proArr containsObject:key]) {
// 如果包含通过 KVC 赋值到 model
[objc setValue:obj forKey:key];
}
}];
return objc;
}
+ (NSArray *)getPropertyArr {
// 获取关联对象
NSArray *proArr = objc_getAssociatedObject(self, key);
if (proArr) return proArr; // 如果有值,直接返回
// 调用运行时方法,获取类的属性列表
/* 成员变量:
* class_copyIvarList(__unsafe_unretained Class cls, unsigned int *outCount)
* 方法:
* class_copyMethodList(__unsafe_unretained Class cls, unsigned int *outCount)
* 属性:
* class_copyPropertyList(__unsafe_unretained Class cls, unsigned int *outCount)
* 协议:
* class_copyProtocolList(__unsafe_unretained Class cls, unsigned int *outCount)
*/
unsigned int count = 0;
// retain, creat, copy 需要release
objc_property_t *property_List = class_copyPropertyList([self class], &count);
NSMutableArray *mtArr = [NSMutableArray array];
// 遍历属性列表, 获取属性名称
for (int i = 0; i < count; i ++) {
objc_property_t pro = property_List[i];
const char *proName_c = property_getName(pro);
NSString *proName = [NSString stringWithCString:proName_c encoding:NSUTF8StringEncoding];
[mtArr addObject:proName];
}
// 设置关联对象
objc_setAssociatedObject(self, key, mtArr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
free(property_List);
return mtArr;
}
@end
核心就是可以遍历出字典中的每个属性,json解析中大牛框架都用了这个特性,包括MJEXtension,YYModel,jsonModel都是将json转换为字典,再遍历字典中的每个属性来进行modle的转换
浙公网安备 33010602011771号