I'm receiving some response from JSON
, and is working fine, but I need to check for some null
values,
I have found different answers but seems is not working still,
NSArray *productIdList = [packItemDictionary objectForKey:@"ProductIdList"];
I have tried with
if ( !productIdList.count ) //which breaks the app,
if ( productIdList == [NSNull null] ) // warning: comparison of distinct pointer types (NSArray and NSNull)
So what is happening? How to fix this and check for null
in my array?
Thanks!
You can use the
isEqual
selector:you should be clear what you want to check: the array is null which means the variable doesn't exist:
Or the array has zero element which you can :
Eliminate the warning using a cast:
If
productIdList
is in fact[NSNull null]
, then doingproductIdList.count
will raise an exception becauseNSNull
does not understand thecount
message.You can also check class of an object by using method
isKindOfClass:
.For example, in your case you could do following:
or (if you are sure that
NSNull
is indicating invalid value)