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?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
StanLe
  • 5,037
  • 9
  • 38
  • 41
  • Just check whether you are allocing and initing NSMutableArray correctly..It seems you are initing it as NSArray (which doesnt have removeObjectAtIndex function) – Krishnabhadra Jul 13 '11 at 04:38
  • it's an NSMuteableArray...initialized and declared as so. – StanLe Jul 13 '11 at 04:44
  • please check, is your mutablearray is going out of scope or is being reassigned somewhere in the code. – Ravin Jul 13 '11 at 05:03
  • Show us where you are assigning your array, the line that goes like `myArray = [Something something]` – EmilioPelaez Jul 13 '11 at 05: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. – Caleb Jul 13 '11 at 05:35

6 Answers6

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]];
mttrb
  • 8,297
  • 3
  • 35
  • 57
Martin Jacob
  • 356
  • 1
  • 7
  • 16
7

The object is an NSArray, not an NSMutableArray.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
  • It's an NSMuteableArray...initialized and declared as so. – StanLe Jul 13 '11 at 04:44
  • 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. – ThomasW Jul 13 '11 at 05:58
  • @Caleb below has the answer as to why your NSArray isn't an NSMutable array. Also see the following: http://stackoverflow.com/questions/3220120/nsmutablearray-addobject-nsarrayi-addobject-unrecognized-selector-sent-to – ThomasW Jul 13 '11 at 08:04
  • Maybe you need to define it as a NSMutableArray in the interface? – Ryan Jan 31 '13 at 23:03
  • @rncrtr The problem is not the interface, the problem is with where the instance is actually allocated or assigned. – ThomasW Feb 06 '13 at 02:39
4

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

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
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.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
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.

Maorco
  • 1