【iOS问题】字典转模型,属性个数不匹配问题
一、字典转模型的键值对与模型属性不匹配问题
1. 字典的键个数 < 模型的属性个数 (key 能与模型的属性匹配)
1> .KVO 方式:
- setValuesForKeysWithDictionary:
2> for循环的方式,一一赋值
2.字典的键个数 = 模型的属性个数 (key 能与模型的属性匹配)
同1。
3.字典的个数 > 模型的属性个数 (模型的属性为字典key 的其中一部分)
一共有三种解决方式
二、解决办法:
建立一个GXApp的模型,申明两个属性: name(名称) details(详细信息),在类方法中是直接使用
在控制器中使用:
直接运行:
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<GXApp 0x7ff1a8d41790> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key version .'
解决办法一:
在类方法中,一个个属性赋值
// // GXApp.m // B01-字典转模型的属性不匹配问题 // // Created by gxiangzi on 15/8/25. // Copyright (c) 2015年 hqu. All rights reserved. // #import "GXApp.h" @implementation GXApp + (instancetype)appWithDict:(NSDictionary*)dict { GXApp* app = [[GXApp alloc] init]; // 利用数组存储 模型的属性 NSArray* parameters = @[ @"name", @"details" ]; // 遍历字典,判断模型是否有该属性 [parameters enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop) { if (dict[obj]) { [app setValue:dict[obj] forKey:obj]; } }]; return app; } @end
缺点: 该种方式对极少数属性相对简单,但是扩展性不高。
解决办法二
运行是机制,在运行时,判断模型的属性,然后赋值
// // GXApp.m // B01-字典转模型的属性不匹配问题 // // Created by gxiangzi on 15/8/25. // Copyright (c) 2015年 hqu. All rights reserved. // #import "GXApp.h" #import <objc/runtime.h> @implementation GXApp + (instancetype)appWithDict:(NSDictionary *)dict { GXApp *app = [[GXApp alloc] init]; NSArray *array = [app getProperties]; // 根据属性的值,去数据字典中取对应的值 [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { // key 属性值. NSString *key = obj; if (dict[key]) { [app setValue:dict[key] forKey:key]; } }]; return app; } // 动态的获取某一个类的属性. - (NSArray *)getProperties { unsigned int count; // 获取一个类中的属性 objc_property_t *properties = class_copyPropertyList(self.class, &count); NSMutableArray *array = [NSMutableArray array]; // 遍历类中的属性,将每一个属性值都转换成 OC 的字符串 ; i < count; i++) { // pro 依然是 C 语言的数据类型 objc_property_t pro = properties[i]; // 指向C 语言字符串一个指针. const char *name = property_getName(pro); NSString *property = [[NSString alloc] initWithUTF8String:name]; [array addObject:property]; } return array; } @end
缺点:运行时代码,C语言代码,代码不易记住
解决办法三、
重写 - setValuesForKeysWithDictionary:
// // GXApp.m // B01-字典转模型的属性不匹配问题 // // Created by gxiangzi on 15/8/25. // Copyright (c) 2015年 hqu. All rights reserved. // #import "GXApp.h" @implementation GXApp +(instancetype)appWithDict:(NSDictionary *)dict { GXApp *app = [[GXApp alloc] init]; [app setValuesForKeysWithDictionary:dict]; return app; } - (void)setValue:(id)value forUndefinedKey:(NSString *)key { // do nothing } @end
解释:
官方解释:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key;
Given that an invocation of -setValue:forKey: would be unable to set the keyed value because the type of the parameter of the corresponding accessor method is an NSNumber scalar type or NSValue structure type but the value is nil, set the keyed value using some other mechanism. The default implementation of this method raises an NSInvalidArgumentException. You can override it to map nil values to something meaningful in the context of your application.
当利用kvo赋值的适合,如果键值不匹配,就会报一个 NSInvalidArgumentException 异常,可以重写这个方法可以解决
【iOS问题】字典转模型,属性个数不匹配问题的更多相关文章
- iOS开发—字典转模型,KVC设计模式
iOS开发UI基础—字典转模型 开发中,通常使用第三方框架可以很快的实现通过字典转模型,通过plist创建模型,将字典的键值对转成模型属性,将模型转成字典,通过模型数组来创建一个字典数组,通过字典数组 ...
- iOS-字典转模型(单模型)的实现
用模型取代字典的好处 使用字典的坏处 一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编译器不会有任何友善提示,需要手敲, eg:dict[@"name&quo ...
- ios中字典转模型的创建以及简单用法
// appModel.h // Created by zzqqrr on 17/8/19. // #import <Foundation/Foundation.h> @interface ...
- ios 根据字典自动生成属性
- (void)createPropertyCode{ NSMutableString *codes = [NSMutableString string]; // 遍历字典 [self enumera ...
- iOS开发——高级技术精选OC篇&Runtime之字典转模型实战
Runtime之字典转模型实战 如果您还不知道什么是runtime,那么请先看看这几篇文章: http://www.cnblogs.com/iCocos/p/4734687.html http://w ...
- 字典转模型框架 Mantle的使用:国外程序员最常用的iOS模型
Mantle简介 Mantle 是iOS和Mac平台下基于Objective-C编写的一个简单高效的模型层框架. Mantle能做什么 Mantle可以轻松把JSON数据.字典(Dictionary) ...
- iOS开发——网络篇——JSON和XML,NSJSONSerialization ,NSXMLParser(XML解析器),NSXMLParserDelegate,MJExtension (字典转模型),GDataXML(三方框架解析XML)
一.JSON 1.JSON简介什么是JSONJSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典 ...
- iOS开发UI篇—字典转模型
iOS开发UI篇—字典转模型 一.能完成功能的“问题代码” 1.从plist中加载的数据 2.实现的代码 // // LFViewController.m // 03-应用管理 // // Creat ...
- IOS开发UI基础之Plis文件-字典转模型
什么是plist文件? 在开发中直接将数据写在代码里面 不是一种合理的做法 如果数据经常改变 就需要经常翻开对应的代码进行修改 造成代码扩展性低 因此,可以考虑将经常变的数据放在⽂文件中进⾏行存储,程 ...
随机推荐
- Wii硬盘版玩机心得
若干年前在电玩巴士买了一台硬盘版的Wii,下面是我的玩机心得: 查看Wii的系统版本信息 链接:http://www.cnblogs.com/duxiuxing/p/4251693.html Wii硬 ...
- date命令使用总结【转载】
本文转载自:http://blog.sina.com.cn/s/blog_674b5aae0100o0w9.html 由于跨年.跨月.闰平年等特殊性,在日常编程过程中对日期的处理总是异常麻烦.目前,各 ...
- Mysql 的字符编码机制、中文乱码问题及解决方案【转载】
本文转载自:http://hi.baidu.com/huabinyin/item/7f51e462df565c97c4d24929.感谢作者及相关博主. 相信很多朋友都会对字符编码敬而远 ...
- Android testing tools
引言 发现一篇关于android 测试的培训,英文的,很全面. Android Testing Training: http://www.vogella.com/training/android/an ...
- tomcat : Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException:
错误 严重: Error configuring application listener of class org.springframework.web.context.ContextLoader ...
- IOS学习教程
http://edu.51cto.com/course/course_id-566.html
- IE的CSS相关的BUG(整理一)
本来不想弄这个ie的bug的,真的很想让它快点死掉,可是事与愿违啊,没办法,还是贴出来,以备自用. 这个网页(http://haslayout.net/css/index)上例举了所有的IE和CSS相 ...
- linux groupmems命令
Because users group membership is defined in two different locations, it can be difficult to find ou ...
- Linux命令之进程的管理
1.进程介绍 进程的分类: 进程一般分为交互进程.批处理进程和守护进程三类. 守护进程总是活跃的,一般是后台运行,守护进程一般是由系统在开机时通过脚本自动激活启动或由超级管理用户root来启动.比如在 ...
- mysql的limit性能,数据库索引问题,dblog问题
mysql的limit性能,数据库索引问题,dblog问题,redis学习 继续学习. dblog实际上是把日志记录在另一个数据库里面. 问题1: 一张表定义了5个索引,但是sql语句中用到了3个有索 ...