Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if an object exists at a certain index of an NSMutableArray?

For instance, how could I verify if there is an item at the index 3?

The objects in the NSArray are instantiated from the class "Animal."

like image 348
Justin Copeland Avatar asked Apr 12 '12 04:04

Justin Copeland


2 Answers

Well, since NSMutableArray has to hold non-nil objects, as long as the array is big enough, you know there's something at index i:

if ([myArray count] > 3) {
    id myObj = [myArray objectAtIndex:3];
    ...
}

If you needed to check something elsek, like say make sure it didn't have a reference to the NSNull singleton, you could then check

if (myObj != [NSNull null]) ...
like image 116
ckhan Avatar answered Nov 18 '22 23:11

ckhan


Since there can be no 'gaps' in a NSMutableArray's storage, if your index is less than [array count], you can be certain an object is present at that index.

like image 5
Extra Savoir-Faire Avatar answered Nov 19 '22 00:11

Extra Savoir-Faire