码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-05 17:23:04      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:ios开发   nsnull   

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

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,赋值:

<span style="font-size:18px;">    NSString *address = [dic objectForKey:@"address"];
    //NSString *area = [dic objectForKey:@"area"];
    NSString *name = [dic objectForKey:@"name"];
    //NSString *city = [dic objectForKey:@"city"];
    NSString *phone = [dic objectForKey:@"phone"];
    //NSString *province = [dic objectForKey:@"province"];
    self.addressLabel.text = address;
    self.nameLabel.text    = name;
    self.phoneLabel.text   = phone;

</span>

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

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

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

<span style="font-size:18px;">    if (![address isKindOfClass:[NSNull class]]) {
        self.addressLabel.text = address;
    }
    if (![name isKindOfClass:[NSNull class]]) {
        self.nameLabel.text    = name;
    }
    if (![phone isKindOfClass:[NSNull class]]) {
        self.phoneLabel.text   = phone;
    }
</span>
这样就OK了

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

标签:ios开发   nsnull   

原文地址:http://blog.csdn.net/feixiang_song/article/details/41749679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!