NSMutableArray removeObjectAtIndex

I get the following error when deleting 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, before deleting, I print out the array and index. Neither one nor the other, and I have no reason to believe why this error will occur. Any ideas?

+6
source share
6 answers

I had this problem. It was mine that I accidentally used type casting as follows.

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

This will work for a while ... but if you are in a tight and big loop, this usually fails.

I changed my code to

 NSMutableArray * myarray= [[NSMutablearray alloc] initWithArray:[mydictionary allKeys]]; 
+8
source

The object is an NSArray , not an NSMutableArray .

+7
source

You call removeObjectAtIndex on an NSArray instance. We can clearly see your crash log.

+4
source

The error indicates that you are trying to call the removeObjectAtIndex selector in an NSArray that will not respond to this selector.

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

+4
source

At this point, four smart people (not including me) indicated that you are sending -removeObjectAtIndex: object that considers it an immutable array. This would be a good time to start wondering why the array is immutable when you considered it volatile. If you post code that shows how the array is created, someone here will probably be able to show you what is going wrong.

One way you can get an immutable array when you think you have a mutable is to copy the modified array. For example, you might have a property:

 @property (copy) NSMutableArray *myArray; 

Perhaps you will 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 have not tested recently, but I'm sure you will get an immutable array. You will definitely get an immutable array if you say:

 NSMutableArray *foo = [tempArray copy]; 

So, start looking for places in the code where your array pointer is reassigned. After all, if your pointer really pointed to a mutable array, it would be terribly difficult to explain the exception you are getting.

+4
source

I had the same problem and this was due to using the copy method. I did one in my own way, returning NSMutableArray *, and it worked.

0
source

Source: https://habr.com/ru/post/892638/


All Articles