11

I have an NSMutableArray which can hold several objects. I would like to check if an object exists, and if so, alter it. I was wondering about checking it. I thought this would work:

if ([[[self.myLibrary objectAtIndex:1] subObject] objectAtIndex:1]) // do something

However, this will crash if there aren't any subObjects at index 1. So I guess the problem is that the above does not return nil if there isn't anything at this Index.

Is there another easy way to check or will I have to count through the array etc.? I know there are other posts on stackoverflow on this, but I haven't found a simple answer yet.

Any explanations / suggestions welcome. Thanks!

1
  • By something, do you mean some index? Commented Jul 22, 2011 at 14:27

4 Answers 4

48

No check simply using :

[myArray indexOfObject:myObject];

or

[myArray containsObject:myObject];

These methods check every object using isEqual.


For example:

NSUInteger objIdx = [myArray indexOfObject: myObject];
if(objIdx != NSNotFound) {
    id myObj = [myArray objectAtIndex: objIdx];
    // Do some alter stuff here
}
Sign up to request clarification or add additional context in comments.

6 Comments

i see. so there isn't a way to check a specific position in a NSMutableArray then.
Jup [myArray indexOfObject:myObject];gives you the position
use indexOfObject: and check the result to be equal to the index you want
Upvoting. Have a pending edit with some code example. Hope you don't mind @Vince!
@Perception - no problem ! I was about to edit myself. but you are to fast for me ;)
|
13

If this is a pattern you use a lot, you could add a category to NSArray called something like safeObjectAtIndex:, which takes an index, does the bounds checking internally:

-(id)safeObjectAtIndex:(NSUInteger)index {
  if (index >= [self count])
    return nil;
  return [self objectAtIndex:index];
}

2 Comments

Added this to NSArray and NSMutableArray, very useful thanks.
Genius!! Nice Idea. !! Working fine.
3

Assuming the object you are using to search with and the actual object in the array are the exact same instance, and not two different objects that are equal according to an overridden isEqual: method, you can do this:

if ([array containsObject:objectToSearchFor]) {
    // modify objectToSearchFor
}

If the two objects are different instances which are equal according to isEqual:, you will have to use code like this:

NSUInteger index = [array indexOfObject:objectToSearchFor];
if (index != NSNotFound) {
    id objectInArray = [array objectAtIndex:index];
    // modify objectInArray
}

Comments

2

NSArray (which is the NSMUtableArray superclass) has lots of methods for finding objects. Have a look at the documentation.

You can either rely on the equals method (e.g. indexOfObject:) or provide a block (e.g indexOfObjectPassingTest:) which is pretty funky.

It's fairly common in Objective C to be using the Mutable version of a class but rely on methods in the non mutable superclass so it's always a good idea when checking the online documentation to look at the superclass.

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.