How to tell if an NSArray is empty (or null) using IF

2

I need to know if an NSArray is empty or null using an if.

Code:

NSArray* array;

 if( array == nil)
{ 
   // Fazer algo
}
    
asked by anonymous 08.09.2014 / 02:58

2 answers

4

As in objective -c a message for nil returns nil , you can use the code below:

NSArray *array = [obtem o valor];
if ([array count]) {
    // Nao é vazio nem nulo
}

If the array is nil , then the result of [array count] will also be nil (which is considered "false" in if). If it is not nil , then the count method returns the number of elements. If it is zero, the if will not go either, since zero is also considered false.

    
08.09.2014 / 03:03
1

Well, it was faster than I thought.

I used NULL instead of nil, thus:

NSArray* array;

 if( array == NULL)
{ 
   // Fazer algo
}
    
08.09.2014 / 03:05