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
.
The NULL value for Objective-C objects (type id) is nil.
While NULL is used for C pointers (type void *).
(In the end both end up holding the same value (0x0). They differ in type however.)
In Objective-C:
So to check against NSNull one can either use:
or if one wants to omit the need of casting to NSNull:
Just pass your string in Method :)
Add an additional check for length also. This will definitely work.
SAFESTRING("PASS_OBJECT_HERE");