(iOS开发)Model中数据多怎么办,JSON中数据多,种类多的解析Model
来源:互联网 发布:淘宝网购物女裤 编辑:程序博客网 时间:2024/05/29 09:37
//model是自己的类型 ,dic是json中所需要解析出来的字典[model setValuesForKeysWithDictionary:dic];//1、如果model里面的有不存在于dic中的元素会怎样?//在Model文件中添加一行@property (nonatomic,copy)NSString *other;//并输出得时候输出NSLog(@"test.other=%@",test.other);//2.如果dic里面的有不存在于model中的元素会怎样?//在Model文件中删除一行@property (nonatomic,copy) NSString* age;//在删除对应得输出后运行。//糟了!通过了编译,但是运行时报错!Terminating app due to uncaught exception 'NSUnknownKeyException',reason: '[<PersonModel 0x7fd731517910> setValue:forUndefinedKey:]:this class is not key value coding-compliant for the key age.' //因为在model中,没有对应的age属性,所以导致了程序崩溃。 //解决方式就是实现一个方法setValue:forUndefinedKey: 这个方法能过滤掉不存在的键值。 //在model中添加。 //h文件中添加: -(void)setValue:(id)value forUndefinedKey:(NSString *)key;//并需要在m文件中实现:-(void)setValue:(id)value forUndefinedKey:(NSString *)key{ }//3.如果dic中的key与model中的变量名字不同,应该怎么赋值?//从前面我们可以知道,dic中key赋值给model中与key同名的属性。//那么如果dic中得key值为 username,model中的名字为name,又或是dic中的key值为ID,INT 等关键字,应该怎么变化。//答案也是从setValue:forUndefinedKey方法入手。//首先我们把dic的值改变:NSDictionary *dic = @{@"username":@"张三",@"sex":@"男",@"id":@"22"};//model中的属性:@property (nonatomic,copy)NSString *name;@property (nonatomic,copy)NSString *sex;@property (nonatomic,copy) NSString* age;//完善model中的setValue:forUndefinedKey方法-(void)setValue:(id)value forUndefinedKey:(NSString *)key{ if([key isEqualToString:@"id"]) { self.age=value; } if([key isEqualToString:@"username"]) { self.name=value; }}//运行后结果:2015-10-19 14:30:11.241setValuesForKeysWithDictionary[10289:956012] test.name=张三2015-10-19 14:30:11.242setValuesForKeysWithDictionary[10289:956012] test.sex=男2015-10-19 14:30:11.242setValuesForKeysWithDictionary[10289:956012] test.age=22//正常输出!
转载自:http://www.jianshu.com/p/870eb4b4170a
0 0
- (iOS开发)Model中数据多怎么办,JSON中数据多,种类多的解析Model
- ios 后台返回json数据里还有数组集合时,model的配置,model套model
- iOS中model 转json
- iOS开发中对JSON解析数据的处理
- IOS开发中model的简单使用
- TableView中Model层数据
- IOS中JSON数据的解析
- iOS中JSON数据解析
- IOS中JSON数据解析
- iOS中解析json数据
- iOS中JSON数据解析
- 解析json存储到model中
- JavaWeb开发中form、ajax提交数据Model转化
- 数据解析及model的封装
- ASP.NET MVC3.0中同一View如何返回多个Model或数据集
- JavaEE_Mybatis_SpringMVC_SpringMVC_SpringMVC的Model中数据的作用域(位置)
- IOS 数据转Model的实现
- thymeleaf中javascript使用model中的数据
- MFC 解析xml文件
- initialize和load方法的使用和比较
- [POJ1741]Tree(点分治)
- 网络编程的几个函数
- linux 下安装配置xampp环境
- (iOS开发)Model中数据多怎么办,JSON中数据多,种类多的解析Model
- C/C++动态分配与释放内存的区别详细解析
- Android 倒计时
- Android 使用代码指定view组件在布局中的属性
- iOS - 高德地图API简单开发(一)
- Xcode8 及 iOS 10 的适配
- vnc model startup script
- FastJSON序列化的使用
- linux服务器测试工具