【发布时间】:2018-12-22 14:13:54
【问题描述】:
我有一个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];
}
它总是执行else 条件,而不是if 条件,即使值是NULL。
【问题讨论】:
-
显示你的空值。
-
由于 NSNull 是一个单例,你实际上可以直接比较指针:if (appDelegate.categoryName == [NSNull null])
-
而不是比较指针用于检查它是 isKindOfClass[NSNull Class]。看我的回答