S.P.
S.P.

Reputation: 5435

How to check if an instance of NSMutableArray is null or not

How to check an NSMutableArray is null or not ?

Upvotes: 24

Views: 34566

Answers (4)

D.M
D.M

Reputation: 530

You can check this way also...

if self.yourMutableArray.count == 0 {
     // Your Mutable array is empty. 
} else {
    // Your Mutable array is not empty.
}

Upvotes: 0

saurabh rathod
saurabh rathod

Reputation: 1268

There are multiple ways to check it.

  1. if (array == [NSNull null])
    {
        //myarray is blank
    }
    
  2. if(array.count==0)
    {
        //myarray is blank
    }
    
  3. if(array == nil)
    {
        //my array is blank
    }
    

Upvotes: 0

user1531494
user1531494

Reputation:

//if the array has a count of elements greater than 0, then the array contains elements
if(myarray.count>0){
    NSlog(@"myarray contains values/elements");
}
else{ //else the array has no elements
    NSlog(@"myarray is nil");
}

Upvotes: 0

Alex Reynolds
Alex Reynolds

Reputation: 96937

If you want to check if it is empty:

if ([myMutableArray count] == 0) { ... }

If you want to check if the variable is nil:

if (!myMutableArray) { ... }

or:

if (myMutableArray == nil) { ... }

Upvotes: 80

Related Questions