I have an NSString
and I want to check if it has a NULL
value. If it does, then the if
condition should execute. Else it should execute the else
condition.
Below is the code which I am using:
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];
}
It always executes the else
condition, and never the if
condition, even when the value is NULL
.
Use the following code:
In Example -1, myString contains nothing. So the output is:
In Example -2, sampleString contains some value. So the output is:
You can do it by using -
[NSNull class]
Its better to be on safer side in checking null values , as it can lead to crash.
In Objective-C and Cocoa, the property may not be set—that is, it's
nil
—or it may be set to the object representation ofnil
, which is an instance ofNSNull
. You probably want to check for either of these conditions, like this:This will execute the nil branch if the
categoryName
property is set tonil
(the default for all properties), or if it's been explicitly set to theNSNull
singleton.Try to use this. Check your value is kind of NULL class or not rather than comparing Pointers value.