Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios check if nsarray == null

Tags:

null

ios

nsarray

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!

like image 836
manuelBetancurt Avatar asked Mar 01 '12 06:03

manuelBetancurt


2 Answers

Eliminate the warning using a cast:

if (productIdList == (id)[NSNull null])

If productIdList is in fact [NSNull null], then doing productIdList.count will raise an exception because NSNull does not understand the count message.

like image 125
rob mayoff Avatar answered Oct 20 '22 01:10

rob mayoff


You can also check class of an object by using method isKindOfClass:.

For example, in your case you could do following:

if ([productIdList isKindOfClass:[NSArray class]])
{
     // value is valid
}

or (if you are sure that NSNull is indicating invalid value)

if([productIdList isKindOfClass:[NSNull class]])
{
     // value is invalid
}
like image 27
Nekto Avatar answered Oct 19 '22 23:10

Nekto