8

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."

3 Answers 3

21

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]) ...
Sign up to request clarification or add additional context in comments.

Comments

5

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.

2 Comments

They could be null, though, right (but that is easy to check, too)?
No NULLs (or nils) in the array, though there can be NSNull objects.
-2

try this code

for(int j = 0; j < [yourArray count]; j++)
{
    if(obj isKindOfClass:[Animal class]]) {
        return
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.