零碎知识点整理
来源:互联网 发布:生姜怎样治疗脱发 知乎 编辑:程序博客网 时间:2024/05/21 18:57
1、服务器返回null、<null>的处理
m_result为服务器返回值
if([m_result isEqual:[NSNUll null]] ||m_result == nil )
{
//处理数据,返回自己想要的处理结果
}
2、字典的快速赋值 setValuesForKeysWithDictionary
PersonModel.h文件中
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *sex;
@property (nonatomic,copy)NSString *age;
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *sex;
@property (nonatomic,copy)NSString *age;
PersonModel *test=[[PersonModel alloc]init];
[test setValuesForKeysWithDictionary:dic];
1、如果model里面的有不存在于dic中的元素会怎样?
@property (nonatomic,copy)NSString *other;
并输出得时候输出
NSLog(@"test.other=%@",test.other);
setValuesForKeysWithDictionary[9964:928391] test.other=(null)
dic中得值可以完全赋值给model,而other没有被赋值,所以值是空的。
2、如果dic里面的有不存在于model中的元素会怎样?
通过了编译,但是运行时报错! 导致程序崩溃!
解决由于此原因导致的崩溃
在model中添加
-(void)setValue:(id)value forUndefinedKey:(NSString *)key;
并需要在m文件中实现
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
3、如果dic中的key与model中的变量名字不同,应该怎么赋值?
NSDictionary *dic = @{@"username":@"张三",@"sex":@"男",@"id":@"22"};
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
if([key isEqualToString:@"id"])
{
self.age=value;
}
if([key isEqualToString:@"username"])
{
self.name=value;
}
}
[test setValuesForKeysWithDictionary:dic];
1、如果model里面的有不存在于dic中的元素会怎样?
@property (nonatomic,copy)NSString *other;
并输出得时候输出
NSLog(@"test.other=%@",test.other);
setValuesForKeysWithDictionary[9964:928391] test.other=(null)
dic中得值可以完全赋值给model,而other没有被赋值,所以值是空的。
2、如果dic里面的有不存在于model中的元素会怎样?
通过了编译,但是运行时报错! 导致程序崩溃!
解决由于此原因导致的崩溃
在model中添加
-(void)setValue:(id)value forUndefinedKey:(NSString *)key;
并需要在m文件中实现
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
3、如果dic中的key与model中的变量名字不同,应该怎么赋值?
NSDictionary *dic = @{@"username":@"张三",@"sex":@"男",@"id":@"22"};
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
if([key isEqualToString:@"id"])
{
self.age=value;
}
if([key isEqualToString:@"username"])
{
self.name=value;
}
}
阅读全文
0 0
- java零碎知识点整理
- DOM零碎知识点整理
- Android 零碎知识点整理
- python 零碎知识点整理
- 零碎知识点整理
- java零碎知识点整理
- H5零碎知识点整理
- 零碎知识点整理
- iOS开发零碎知识点整理
- JavaScript 零碎知识点整理(一)
- 零碎的知识点(未整理)
- java零碎知识点整理(2)
- python零碎知识点整理——注释
- java零碎知识点整理(3)
- 零碎知识点
- 零碎知识点
- 零碎知识点
- 零碎知识点
- pymysql语法记录
- 树链剖分+数据结构维护 Hdu 3966 Aragorn's Story 题解
- For Markdown
- MyBatis第一课:回忆JDBC
- 20170702Windows10_07_线程优先级及CPU亲和度
- 零碎知识点整理
- HDU5934-Bomb
- For markdown
- 常用布局
- 常见的几种Sort排序算法
- 学渣眼中的软件工程(三)
- zeroc-ice的全双工通信策略
- 【MFC】基于对话框的键盘响应
- 单调栈——51nod 1423 最大二“货”