Runtime进阶三:字典转模型
写在前面,这是一篇介绍如何通过Runtime来实现,从请求到数据,到生成模型,再到给模型赋值的全过程。如果只在意结果,可以分别用三方软件生成模型,再用MJExtension等三方库给模型赋值。
首先第一步:请求到数据后生成模型
// 拼接属性字符串代码
NSMutableString *strM = [NSMutableString string];
// 1.遍历字典,把字典中的所有key取出来,生成对应的属性代码
[dict 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";
}
// 属性字符串
NSString *str;
if ([type containsString:@"NS"]) {
if ([type isEqualToString:@"NSString"]) {
str = [NSString stringWithFormat:@"@property (nonatomic, copy) %@ *%@;",type,key];
}else {
str = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@;",type,key];
}
}else{
str = [NSString stringWithFormat:@"@property (nonatomic, assign) %@ %@;",type,key];
}
// 每生成属性字符串,就自动换行。
[strM appendFormat:@"\n%@\n",str];
}];
// 把拼接好的字符串打印出来,就好了。
NSLog(@"%@",strM);
第二步,实现字典转模型
这一步有两种方式,方式一:通过KVC实现
但是这种方式有个弊端,当模型中属性和字典中不一致时,就会报错。
解决途径:重写如下方法,当不一致时,系统会自动调用
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
方式二:通过Runtime实现字典转模型
实现思路:遍历模型中所有属性,根据模型中所有的属性名去字典中查key,给模型的属性赋值
首先第一步,遍历模型中所有的属性
//class_copyIvarList(self, &count)该方法第一个参数是要获取哪个类中的成员属性,第二个参数是这个类中有多少成员属性,需要传入地址,返回值Ivar是个数组,会将所有成员属性放入这个数组中
id objc = [[self alloc] init];
unsigned int count;
// 获取类中的所有成员属性
Ivar *ivarList = class_copyIvarList(self, &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivarList[i];
// 获取成员属性名
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
// 处理成员属性名->字典中的key
// 从第一个角标开始截取
NSString *key = [name substringFromIndex:1];
// 根据成员属性名去字典中查找对应的value
id value = dict[key];
// 判断字典中是否存在字典,如果存在,转为模型字典属性生成的是@"@\"xxxx\""类型,需要裁减为@"xxxx"
if ([value isKindOfClass:[NSDictionary class]]) {
// 获取成员属性类型
NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
// 生成的是这种@"@\"User\"" 类型 -》 @"User" 在OC字符串中 \" -> ",\是转义的意思,不占用字符
// 裁剪类型字符串
NSRange range = [type rangeOfString:@"\""];
type = [type substringFromIndex:range.location + range.length];
range = [type rangeOfString:@"\""];
// 裁剪到哪个角标,不包括当前角标
type = [type substringToIndex:range.location];
// 根据字符串类名生成类对象
Class modelClass = NSClassFromString(type);
if (modelClass) {
value = [modelClass modelWithDict:value];
}
}
//通过给分类添加一个协议,来实现将数组中的字典转为模型
if ([value isKindOfClass:[NSArray class]])
{
// 判断对应类有没有实现字典数组转模型数组的协议
if ([self respondsToSelector:@selector(arrayContainModelClass)]) {
// 转换成id类型,就能调用任何对象的方法
id idSelf = self;
// 获取数组中字典对应的模型
NSString *type = [idSelf arrayContainModelClass][key];
// 生成模型
Class classModel = NSClassFromString(type);
NSMutableArray *arrM = [NSMutableArray array];
// 遍历字典数组,生成模型数组
for (NSDictionary *dict in value) {
// 字典转模型
id model = [classModel modelWithDict:dict];
[arrM addObject:model];
}
// 把模型数组赋值给value
value = arrM;
}
}