CRASH: -[NSNull length]: unrecognized selector sent to instance错误的处理办法

开发中从后台请求数据,返回如下:

2014-12-05 16:44:52.535 掌麦[6984:613] getDefaultAddress: reuslt == {
    item =     {
        data =         {
            address = "<null>";
            area = "<null>";
            city = "<null>";
            name = "";
            phone = 18503051318;
            province = "<null>";
        };
        mark = 1;
        msg = "\U6210\U529f";
    };
    status = 1;
}

我直接解析出字典dic,赋值:

[objc]  view plain copy
  1. <span style="font-size:18px;">    NSString *address = [dic objectForKey:@"address"];  
  2.     //NSString *area = [dic objectForKey:@"area"];  
  3.     NSString *name = [dic objectForKey:@"name"];  
  4.     //NSString *city = [dic objectForKey:@"city"];  
  5.     NSString *phone = [dic objectForKey:@"phone"];  
  6.     //NSString *province = [dic objectForKey:@"province"];  
  7.     self.addressLabel.text = address;  
  8.     self.nameLabel.text    = name;  
  9.     self.phoneLabel.text   = phone;  
  10.   
  11. </span>  

   结果报错:CRASH: -[NSNull length]: unrecognized selector sent to instance

原因及时后台返回有些值为null,在object-c中就是nsull的空值

所以解析后,应加一判断,防止出错

[objc]  view plain copy
  1. <span style="font-size:18px;">    if (![address isKindOfClass:[NSNull class]]) {  
  2.         self.addressLabel.text = address;  
  3.     }  
  4.     if (![name isKindOfClass:[NSNull class]]) {  
  5.         self.nameLabel.text    = name;  
  6.     }  
  7.     if (![phone isKindOfClass:[NSNull class]]) {  
  8.         self.phoneLabel.text   = phone;  
  9.     }  
  10. </span>  

这样就OK了


转载:http://blog.csdn.net/feixiang_song/article/details/41749679

你可能感兴趣的:(CRASH: -[NSNull length]: unrecognized selector sent to instance错误的处理办法)