我在网上找到的大多数例子都写道:
if(x != nil)
// ...
Run Code Online (Sandbox Code Playgroud)
这有什么问题吗?
if(x)
// ...
Run Code Online (Sandbox Code Playgroud)
我在一个简单的程序中尝试了两个,但没有找到任何区别.
我有一段代码检测如果NSString是NULL,nil等等.然而,它崩溃.这是我的代码:
NSArray *resultstwo = [database executeQuery:@"SELECT * FROM processes WHERE ready='yes' LIMIT 0,1"];
for (NSDictionary *rowtwo in resultstwo) {
NSString *getCaption = [rowtwo valueForKey:@"caption"];
if (getCaption == NULL) {
theCaption = @"Photo uploaded...";
} else if (getCaption == nil) {
theCaption = @"Photo uploaded...";
} else if ([getCaption isEqualToString:@""]) {
theCaption = @"Photo uploaded...";
} else if ([getCaption isEqualToString:@" "]) {
theCaption = @"Photo uploaded...";
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
因未捕获的异常而终止应用'
NSInvalidArgumentException',原因:'-[NSNull isEqualToString:]:无法识别的选择器发送到实例 …
我有一个NSString,我想检查它是否有NULL值.如果是,那么if条件应该执行.否则它应该执行else条件.
以下是我使用的代码:
if ([appDelegate.categoryName isEqual:[NSNull null]])
{
select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID where ContentMaster.ContentTagText='%@'", appDelegate.tagInput];
}
else
{
select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID where ContentMaster.ContentTagText='%@' && Category.CategoryName='%@' && Topic.TopicName='%@'", appDelegate.tagInput, appDelegate.categoryName, appDelegate.topicName];
}
Run Code Online (Sandbox Code Playgroud)
它总是执行else条件,而不是if条件,即使值是NULL.