iOS 字典转模型的几中方法、KVC、runtime、YYModel、MJExtention

 

 快速转换,只写一层建模型,在.m文件中取相应层级的模型。

/**
 {"returnStatus":"000","returnMsg":"ok",
 "data":{"accessKeyId":"34d89d72a26511ebb2d1c52b9bdb9fa5","secretAccessKey":"fa713467ed8c409ca26f0dbfb2832235","sessionToken":"ZjkyZm","expiration":"2021-04-21T17:48:32.000+00:00"}
 }
 **/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LYBBDkeymodel : NSObject
@property(nonatomic,copy)NSString *returnStatus;
@property(nonatomic,copy)NSString *returnMsg;
@property(nonatomic,copy)NSString *data;
@property(nonatomic,copy)NSString *accessKeyId;
@property(nonatomic,copy)NSString *secretAccessKey;
@property(nonatomic,copy)NSString *sessionToken;
@property(nonatomic,copy)NSString *expiration;
@end

NS_ASSUME_NONNULL_END





#import "LYBBDkeymodel.h"

@implementation LYBBDkeymodel
//这个是替换字段名,可以是一级,也可以是多级(这样就不用使用多个模型了)
+ (NSDictionary *)mj_replacedKeyFromPropertyName{


    return  @{
              @"returnStatus":@"returnStatus",
              @"returnMsg":@"returnMsg",
              @"returnMsg":@"returnMsg",
              @"accessKeyId":@"data.accessKeyId",
              @"secretAccessKey":@"data.secretAccessKey",
              @"sessionToken":@"data.sessionToken",
              @"expiration":@"data.expiration"
           
              };
}
@end




使用: LYBBDkeymodel *model=[LYBBDkeymodel mj_objectWithKeyValues:responseObject];//responseObject是一个NSDictionary。

1.KVC字典转模型

#import <Foundation/Foundation.h>

 

@interface HeroModel : NSObject

 

@property (nonatomic,copy)NSString *icon;

 

@property (nonatomic,copy)NSString *intro;

 

 

@property (nonatomic,copy)NSString *name;

 

- (instancetype)initWithDict:(NSDictionary *)dict;

 

+ (instancetype)heroModelWithDict:(NSDictionary *)dict;

@end

======================

#import "HeroModel.h"

 

@implementation HeroModel

 

- (instancetype)initWithDict:(NSDictionary *)dict {

    if (self = [super init]) {

        [self setValuesForKeysWithDictionary:dict];

    }

    return self;

}

 

+ (instancetype)heroModelWithDict:(NSDictionary *)dict {

    return [[self alloc]initWithDict:dict];

}

@end

 

==============================

 

- (NSArray *)dataArray {

    if (nil ==_dataArray) {

        // 1. 路径

        NSString *path = [[NSBundlemainBundle]pathForResource:@"heros.plist"ofType:nil];

        

        // 2. 读取内容

        NSArray *tempArray = [NSArrayarrayWithContentsOfFile:path];

        

        // 3. 可变数组

        NSMutableArray *mutable = [NSMutableArrayarray];

        

        // 4. 字典转模型

        for (NSDictionary *dictin tempArray) {

            HeroModel *model = [HeroModelheroModelWithDict:dict];

            

            [mutable addObject:model];

        }

        

        _dataArray = mutable;

        

    }

    return_dataArray;

}

 

 

========================

 

//双模型转换

#import "CarModel.h"

#import "InnerCarModel.h"

 

@implementation CarModel

 

- (instancetype)initWithDict:(NSDictionary *)dict {

    if (self = [superinit]) {

        

        [selfsetValuesForKeysWithDictionary:dict];

        // 经过kvc赋值之后,现在 cars这个数组中有值,而且存放的是字典

        // 1. 定义一个临时可变数组

        NSMutableArray *mutable = [NSMutableArrayarray];

        

        // 2.数组属性转成模型

        for (NSDictionary *dictinself.cars) {

            InnerCarModel *innerModel = [InnerCarModelinnerCarModelWithDict:dict];

            

            // 添加到可变数组中

            [mutable addObject:innerModel];

        }

        

        // 把可变数组赋值给 self.cars , mutable 数组中装的是 InnerCarModel对象

        self.cars = mutable;

    }

    return self;

}

 

+ (instancetype)carModelWithDict:(NSDictionary *)dict {

    return [[selfalloc]initWithDict:dict];

}

KVC字典转模型弊端:必须保证,模型中的属性和字典中的key一一对应。

如果不一致,就会调用[<Status 0x7fa74b545d60> setValue:forUndefinedKey:]

报key找不到的错。

分析:模型中的属性和字典的key不一一对应,系统就会调用setValue:forUndefinedKey:报错。

解决:重写对象的setValue:forUndefinedKey:,把系统的方法覆盖,

就能继续使用KVC,字典转模型了。

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{

} (void)setValue:(id)value forUndefinedKey:(NSString *)key
{

}

 

 

========================

字典转模型的方式二:Runtime

思路:利用运行时,遍历模型中所有属性,根据模型的属性名,去字典中查找key,取出对应的值,给模型的属性赋值。

步骤:提供一个NSObject分类,专门字典转模型,以后所有模型都可以通过这个分类转。

end@implementation NSObject (Model)@implementation NSObject (Model)
+ (instancetype)modelWithDict:(NSDictionary *)dictinstancetype)modelWithDict:(NSDictionary *)dict
{	// 思路:遍历模型中所有属性-》使用运行时
	// 0.创建对应的对象
	id objc = [[self alloc] init];	// 1.利用runtime给对象中的成员属性赋值
	// class_copyIvarList:获取类中的所有成员属性
	// Ivar:成员属性的意思
	// 第一个参数:表示获取哪个类中的成员属性
	// 第二个参数:表示这个类有多少成员属性,传入一个Int变量地址,会自动给这个变量赋值
	// 返回值Ivar *:指的是一个ivar数组,会把所有成员属性放在一个数组中,通过返回的数组就能全部获取到。
	/* 类似下面这种写法
	 Ivar ivar;
	 Ivar ivar1;
	 Ivar ivar2;
	 // 定义一个ivar的数组a
	 Ivar a[] = {ivar,ivar1,ivar2};
	 // 用一个Ivar *指针指向数组第一个元素
	 Ivar *ivarList = a;
	 // 根据指针访问数组第一个元素
	 ivarList[0];
	 */
	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];		// 二级转换:如果字典中还有字典,也需要把对应的字典转换成模型
		// 判断下value是否是字典
		if ([value isKindOfClass:[NSDictionary class]]) {			// 字典转模型
			// 获取模型的类对象,调用modelWithDict
			// 模型的类名已知,就是成员属性的类型
			// 获取成员属性类型
		   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);			// 思路:遍历模型中所有属性-》使用运行时
	// 0.创建对应的对象
	id objc = [[self alloc] init];	// 1.利用runtime给对象中的成员属性赋值
	// class_copyIvarList:获取类中的所有成员属性
	// Ivar:成员属性的意思
	// 第一个参数:表示获取哪个类中的成员属性
	// 第二个参数:表示这个类有多少成员属性,传入一个Int变量地址,会自动给这个变量赋值
	// 返回值Ivar *:指的是一个ivar数组,会把所有成员属性放在一个数组中,通过返回的数组就能全部获取到。
	/* 类似下面这种写法
	 Ivar ivar;
	 Ivar ivar1;
	 Ivar ivar2;
	 // 定义一个ivar的数组a
	 Ivar a[] = {ivar,ivar1,ivar2};
	 // 用一个Ivar *指针指向数组第一个元素
	 Ivar *ivarList = a;
	 // 根据指针访问数组第一个元素
	 ivarList[0];
	 */
	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];		// 二级转换:如果字典中还有字典,也需要把对应的字典转换成模型
		// 判断下value是否是字典
		if ([value isKindOfClass:[NSDictionary class]]) {			// 字典转模型
			// 获取模型的类对象,调用modelWithDict
			// 模型的类名已知,就是成员属性的类型
			// 获取成员属性类型
		   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];
			}
		}		 (modelClass) { // 有对应的模型才需要转
				// 把字典转模型
				value  =  [modelClass modelWithDict:value];
			}
		}		
// 三级转换:NSArray中也是字典,把数组中的字典转换成模型.
		// 判断值是否是数组
		if ([value isKindOfClass:[NSArray class]]) {			// 判断对应类有没有实现字典数组转模型数组的协议
			if ([self respondsToSelector:@selector(arrayContainModelClass)]) {				// 转换成id类型,就能调用任何对象的方法
				id idSelf = self;				
		// 判断值是否是数组
		if ([value isKindOfClass:[NSArray class]]) {			// 判断对应类有没有实现字典数组转模型数组的协议
			if ([self respondsToSelector:@selector(arrayContainModelClass)]) {				// 转换成id类型,就能调用任何对象的方法
				id idSelf = self;				
// 获取数组中字典对应的模型
		NSString *type =  [idSelf arrayContainModelClass][key];				
		NSString *type =  [idSelf arrayContainModelClass][key];				
// 生成模型
			   Class classModel = NSClassFromString(type);				
			   Class classModel = NSClassFromString(type);				
NSMutableArray *arrM = [NSMutableArray array];				// 遍历字典数组,生成模型数组
				for (NSDictionary *dict in value) {					// 字典转模型
				  id model =  [classModel modelWithDict:dict];
					[arrM addObject:model];
				}				// 把模型数组赋值给value
				value = arrM;
			}
		}		if (value) { // 有值,才需要给模型的属性赋值
			// 利用KVC给模型中的属性赋值
			[objc setValue:value forKey:key];
		}
	}	return objc;
}@end
 *arrM = [NSMutableArray array];				// 遍历字典数组,生成模型数组
				for (NSDictionary *dict in value) {					// 字典转模型
				  id model =  [classModel modelWithDict:dict];
					[arrM addObject:model];
				}				// 把模型数组赋值给value
				value = arrM;
			}
		}		if (value) { // 有值,才需要给模型的属性赋值
			// 利用KVC给模型中的属性赋值
			[objc setValue:value forKey:key];
		}
	}	return objc;
}@end

 


=====================================

根据字典生成模型中对应的属性字符串。

@implementation NSObject (Log)// 自动打印属性字符串+ (void)resolveDict:(NSDictionary *)dict{	// 拼接属性字符串代码
	NSMutableString *strM = [NSMutableString string];	// 1.遍历字典,把字典中的所有key取出来,生成对应的属性代码
	[dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {		// 类型经常变,抽出来
		 NSString *type;		 NSObject (Log)// 自动打印属性字符串+ (void)resolveDict:(NSDictionary *)dict{	// 拼接属性字符串代码
	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";
		}		// 属性字符串
		NSString *str;		if ([type containsString:@"NS"]) {
			str = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@;",type,key];
		}else{
			str = [NSString stringWithFormat:@"@property (nonatomic, assign) %@ %@;",type,key];
		}		// 每生成属性字符串,就自动换行。
		[strM appendFormat:@"\n%@\n",str];
	}];	// 把拼接好的字符串打印出来,就好了。
	NSLog(@"%@",strM);
}@end
 ([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";
		}		// 属性字符串
		NSString *str;		if ([type containsString:@"NS"]) {
			str = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@;",type,key];
		}else{
			str = [NSString stringWithFormat:@"@property (nonatomic, assign) %@ %@;",type,key];
		}		// 每生成属性字符串,就自动换行。
		[strM appendFormat:@"\n%@\n",str];
	}];	// 把拼接好的字符串打印出来,就好了。
	NSLog(@"%@",strM);
}@end

/================

LModel中一一个属性

@property(nonatomic,copy)NSString *ss;

    LModel *lm=[[LModelalloc]init];

    [lm setValue:@"ssss"forKey:@"ss"];//setvalue: forkey:也可以字典转模型

    NSLog(@"---%@",lm.ss);


 

****************YYModel

 

******************MJExtention

https://blog.csdn.net/u011146511/article/details/51237025

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值