简体   繁体   中英

NSMutableArray removeObjectAtIndex

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?

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]];

The object is an NSArray , not an NSMutableArray .

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

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM