6

I'm getting the following error when removing from my NSMutableArray

-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x1cdced10
2011-07-13 00:33:14.333 MassText[1726:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x1cdced10'

However right before I remove, I print out the array and the index. Neither are nil and I have no reason to believe why this error would be happening. Any ideas?

5
  • Just check whether you are allocing and initing NSMutableArray correctly..It seems you are initing it as NSArray (which doesnt have removeObjectAtIndex function) Commented Jul 13, 2011 at 4:38
  • it's an NSMuteableArray...initialized and declared as so. Commented Jul 13, 2011 at 4:44
  • please check, is your mutablearray is going out of scope or is being reassigned somewhere in the code. Commented Jul 13, 2011 at 5:03
  • Show us where you are assigning your array, the line that goes like myArray = [Something something] Commented Jul 13, 2011 at 5:30
  • If the array pointer were nil, this exception wouldn't be thrown. And an index isn't a pointer, so it can't be nil. Commented Jul 13, 2011 at 5:35

6 Answers 6

8

I had this problem. Mine was that I accidentally used type casting like this.

NSMutablearray * myarray = [[NSMutableArray alloc] init];
myarray =(NSMutableArray*) [mydictionary allkeys];

This will work for some time.. but if you are in a tight and large loop this tend to fail.

I changed my code to

NSMutableArray * myarray= [[NSMutablearray alloc] initWithArray:[mydictionary allKeys]];
Sign up to request clarification or add additional context in comments.

1 Comment

NSMutableArray is CaseSensitive(Edit your answer)
7

The object is an NSArray, not an NSMutableArray.

5 Comments

It's an NSMuteableArray...initialized and declared as so.
Perhaps the variable type is NSMutableArray, but the instance appears to be an NSArray given the log output. Be sure to double check where the object is actually getting created.
@Caleb below has the answer as to why your NSArray isn't an NSMutable array. Also see the following: stackoverflow.com/questions/3220120/…
Maybe you need to define it as a NSMutableArray in the interface?
@rncrtr The problem is not the interface, the problem is with where the instance is actually allocated or assigned.
4

You are calling removeObjectAtIndex on a NSArray instance. We can see clearly by your crash log.

Comments

4

At this point, four smart people (not including myself) have pointed out that you're sending -removeObjectAtIndex: to an object that thinks it's an immutable array. This would be a good time to start wondering why the array is immutable when you previously thought it was mutable. If you post some code that shows how the array is created, someone here will probably be able to show you what's going wrong.

One way that you can end up with an immutable array when you thought you had a mutable one is to copy a mutable array. For example, you might have a property:

@property (copy) NSMutableArray *myArray;

Perhaps you then create a mutable array, add some objects, and assign it to your property:

NSMutableArray *tempArray = [NSMutableArray array];
[tempArray addObject:@"You say goodbye"];
[tempArray addObject:@"I say hello"];
self.myArray = tempArray;

Now, does tempArray point to a mutable array or an immutable array? I haven't tested recently, but I'm pretty sure that you get an immutable array. You definitely get an immutableArray if you say:

NSMutableArray *foo = [tempArray copy];

So, start looking for places in your code where your array pointer is reassigned. After all, if your pointer really did point to a mutable array, it'd be awfully hard to explain the exception that you're getting.

Comments

4

The error says that you are trying to call the removeObjectAtIndex selector on an NSArray, which won't respond to that selector.

Make sure the array is really an NSMutableArray, not an NSArray.

Comments

0

I had the same problem, and it was because of the use of the copy method. I made one on my own returning a NSMutableArray* and it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.