零碎知识点整理

来源:互联网 发布:生姜怎样治疗脱发 知乎 编辑:程序博客网 时间: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;

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;
        }
}

原创粉丝点击