我正在检查字典中的项目(从服务器返回)是否为 NULL,如下所示:
NSString *imageURL = [userDict objectForKey:@"image_url"];
NSString *userName = [userDict objectForKey:@"username"];
NSString *typeName = [userDict objectForKey:@"type"];
if (imageURL == NULL || userName == NULL || typeName == NULL || imageURL.length == 0) {
localImage = [UIImage imageNamed:@"first.png"];
[addAnnotation setTitle:@"Something"];
[addAnnotation setSubTitle:@"Wrong"];
}
但是,在一个记录中(上面是在一个for循环中)[userDict objectForKey:@"image_url"]
返回 "<null>"
但在上面的 if 语句中没有得到正确处理。为什么我的空检查不起作用?
最佳答案
代替
if([userDict objectForKey:@"image_url"] == nil)
使用这个:
if([userDict objectForKey:@"image_url"] == [NSNull null])
当您处理由 JSON 反序列化程序或类似程序返回的字典时,它工作得很好。
关于iphone - 检查 NULL 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11987144/