我有一个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.
Jas*_*oco 34
在Objective-C和Cocoa中,属性可能没有设置 - 也就是说,nil它可能被设置为对象表示nil,它是一个实例NSNull.您可能想要检查这些条件中的任何一个,如下所示:
NSString* categoryName = appDelegate.categoryName;
if (categoryName == nil || categoryName == (id)[NSNull null]) {
// nil branch
} else {
// category name is set
}
Run Code Online (Sandbox Code Playgroud)
如果categoryName属性设置为nil(所有属性的默认值),或者已将其显式设置为NSNull单例,则将执行nil分支.
| 归档时间: |
|
| 查看次数: |
25467 次 |
| 最近记录: |