【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文件? 在开发中直接将数据写在代码里面 不是一种合理的做法 如果数据经常改变 就需要经常翻开对应的代码进行修改 造成代码扩展性低 因此,可以考虑将经常变的数据放在⽂文件中进⾏行存储,程 ...
随机推荐
- js类型判断及鸭式辨型
目录 instanceof constructor 构造函数名字 鸭式辨型 三种检测对象的类方式: instanceof.constructor .构造函数名字 用法如下: 1)instanceof ...
- collapse
1. border-collapse 该CSS属性用来设定表格的行和列的边框是合并成单边框,还是分别有各自的边框 separate 缺省值.边框分开,不合并. collapse 边框合并.即如果相邻, ...
- Sql自动生成字母加数字的随机数
/* select char(65+ceiling(rand()*25)) --随机字母(大写) select char(97+ceiling(rand()*25)) --随机字母(小写) selec ...
- HDU 4828 - Grids (Catalan数)
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4828 Catalan数的公式为 C[n+1] = C[n] * (4 * n + 2) / (n ...
- [LeetCode] 205. Isomorphic Strings 解题思路 - Java
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- C - Building a Space Station - poj 2031
空间站是有一些球状的房间组成的,现在有一些房间但是没有相互连接,你需要设计一些走廊使他们都相通,当然,有些房间可能会有重合(很神奇的样子,重合距离是0),你需要设计出来最短的走廊使所有的点都连接. 分 ...
- php开启错误提示
1.在php.ini文件里加上下面两句 display_errors = Onerror_reporting = E_ALL | E_STRICT 2.在Apache的 httpd.conf文件里加上 ...
- js打开新的链接2
window.open打开新的连接时可能会被浏览器拦截掉. 所以采用动态创建a标签的形式. var a = document.createElement('a'); a.href = myUrl; ...
- H5页开发规范/通用规范
兼容目标 主流移动设备:iPhone 4+ .三星.魅族.华为.红米.小米1S 以上及主流 Android 千元机型:请特别关注iPhone4/4s.魅族MX4.华为P6等机型 操作系统:iOS 7. ...
- JNI- java.lang.UnsatisfiedLinkError: Native method not found
http://stackoverflow.com/questions/24566127/jni-java-lang-unsatisfiedlinkerror-native-method-not-fou ...