This could be the simplest one but I am not able to find the exact cause. Instead to explain in words following is my code:
NSString *str = @"";
NSInteger tempInteger = [str integerValue];
if (tempInteger == 0)
{
NSLog (@"do something");
}
If NSString returning NULL then NSInteger returning 0 and if NSStringhas some value then NSInteger returning same value.
But when NSString is NULL then I want NSInteger should return NULL not 0 but I am not able to validation NSInteger as NULL.
How can I get NULL value in NSInteger if NSString is NULL?
NULLis a pointer; NSInteger is a C primitive that can only hold numbers. It is not possible for an NSInteger to ever beNULL. (Sidenote: when we speak of Objective-C object pointers, we usenilrather thanNULL.)Instead, if you wish to handle only cases where the integer value is 0, but the string is not
NULL, you should change your condition as follows:However, in your case, the string is not
nil; it is merely an existent–but empty–string. If you wish to also check for that, your condition could become:This checks both cases because a
nilstring will return length0. (In fact, any method sent tonilwill return0,nil,0.0, etc. depending on the return type.)