首页 > 代码库 > CRASH: -[NSNull length]: unrecognized selector sent to instance错误的处理办法
CRASH: -[NSNull length]: unrecognized selector sent to instance错误的处理办法
开发中从后台请求数据,返回如下:
2014-12-05 16:44:52.535 掌麦[6984:613] getDefaultAddress: reuslt == {
item = {
data =http://www.mamicode.com/ {
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错误的处理办法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。