开发中从后台请求数据,返回如下:
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了
时间: 2024-10-29 09:52:46