如何在iOS中通过`json`解释器检查`null`对象

时间:2013-09-09 11:36:58

标签: iphone ios objective-c nsnull

更新:我认为错误来自数据Feed中缺少的密钥,而不是具有空值的密钥。

我一直在null解释器遇到json对象,并认为此代码会进行错误检查并避免崩溃。虽然不行。我需要检查以避免此错误消息并崩溃:

imageurl string is <null>
2013-09-09 12:35:26.928 1000 [1539:690b] -[NSNull length]: unrecognized selector sent to instance

if (![imageUrlString isKindOfClass:[NSNull class]])
{

    imageUrlString = [userImageArray objectAtIndex:indexPath.row];


} else {

    cell.thumbnailImageView.image = [UIImage imageNamed:@"rest.png"];

}

fyi - 如果图像字符串存在并且仅在遇到空对象时崩溃,则它可以工作。

nslog的输出:

遇到null obj时:imageurl string is (null)

工作时:imageurl string is http://******.com/uploads/users/35/profile.jpeg

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题。它不认为您对NSNull的检查不是检查null的正确技术。尝试使用第一个if语句中的一个检查:

/* Either of these checks in the if statement should work */
if ((NSNull *)imageUrlString != [NSNull null] || [@"<null>" isEqualToString:imageUrlString]) {
    imageUrlString = [userImageArray objectAtIndex:indexPath.row];
} else {
    cell.thumbnailImageView.image = [UIImage imageNamed:@"rest.png"];
}

答案 1 :(得分:0)

它崩溃了,因为NSNull不是一种“类”。

您需要检查对象本身是否为空,并且同时检查它是否为空也可能是个好主意:

if (imageUrlString!=[NSNull null] && [imageUrlString length]>0){
    imageUrlString = [userImageArray objectAtIndex:indexPath.row];
} else {
    cell.thumbnailImageView.image = [UIImage imageNamed:@"rest.png"];
}