Ios NSNull isEqualToString:ObjC上无法识别的选择器
我的代码是这样的:Ios NSNull isEqualToString:ObjC上无法识别的选择器,ios,objective-c,string,Ios,Objective C,String,我的代码是这样的: cell.datePlace.text= [NSString stringWithFormat:@"%@, %@",date,place]; "21/10/2012, <null>" "21/11/2012, None" "21/12/2013, London" 我有两个字符串,地点和日期。我这样使用它们: cell.datePlace.text= [NSString stringWithFormat:@"%@, %@",date,place]; "21/1
cell.datePlace.text= [NSString stringWithFormat:@"%@, %@",date,place];
"21/10/2012, <null>"
"21/11/2012, None"
"21/12/2013, London"
我有两个字符串,地点和日期。
我这样使用它们:
cell.datePlace.text= [NSString stringWithFormat:@"%@, %@",date,place];
"21/10/2012, <null>"
"21/11/2012, None"
"21/12/2013, London"
在某些条目中,输出如下所示:
cell.datePlace.text= [NSString stringWithFormat:@"%@, %@",date,place];
"21/10/2012, <null>"
"21/11/2012, None"
"21/12/2013, London"
问题是当place是
时,我的应用程序崩溃了,我出现了以下错误:
[NSNull isEqualToString:] unrecognized selector send to instance
所以,我试过这个:
NSString * place=[photo objectForKey:@"place"];
if ([place isEqualToString:@"None"]) {
cell.datePlace.text= [NSString stringWithFormat:@"%@",date];
} else {
cell.datePlace.text= [NSString stringWithFormat:@"%@, %@",date,place];
}
if (place) {
if ([place isEqualToString:@"None"]) {
cell.datePlace.text= [NSString stringWithFormat:@"%@",date];
} else {
cell.datePlace.text= [NSString stringWithFormat:@"%@, %@",date,place];
}
} else {
cell.datePlace.text= [NSString stringWithFormat:@"%@",date];
}
但问题仍然存在。使用[NSNull]:
if ([place isKindOfClass:[NSNull class]])
{
// What happen if place is null
}
使用
而不是
if (place) {
...
}
注意:NSNull对象不是nil,因此
if(place)
将为true。我猜您的源数据来自JSON或类似文件(其中数据被解析,缺少的数据被设置为NSNull
)。您需要处理的是NSNull
,目前还没有
基本上:
if (place == nil || [place isEqual:[NSNull null]]) {
// handle the place not being available
} else {
// handle the place being available
}
是的,它来自JSON。它现在正在工作。我会接受答案!请注意,
NSNull
是一个单例对象,因此您只需与place==[NSNull null]
进行比较即可无检查是多余的。