Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray removeObjectAtIndex cause error [closed]

This is probably an simple problem but would someone nice be able to give me a hint what is wrong in the following scenario, i just do not understand. I have a NSMutableArray "playerArray", which have objects "ZERO", "ONE", "TWO", THREE".

I am trying to remove the object at "row" but it does not work. Could it be so that it returns a immutable array as the exception seems to getting thrown because it doesn't respond to removeObjectAtIndex?

 NSUInteger row = 1; // [indexPath row];
 NSLog(@"playerArray:%@", playerArray);
 NSLog(@"row: %i", row);
 [playerArray removeObjectAtIndex:row];  

The result is:

playerArray:(
ZERO,
ONE,
TWO,
THREE
)
2010-11-21 20:58:46.681 FamQuiz_v2[2166:207] row: 1
2010-11-21 20:58:46.682 FamQuiz_v2[2166:207] -[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x5e3b9a0
2010-11-21 20:58:46.683 FamQuiz_v2[2166:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x5e3b9a0'

like image 664
PeterK Avatar asked Nov 21 '10 20:11

PeterK


2 Answers

It looks like your array is not actually a NSMutableArray. Verify it by logging the class name:

NSLog(NSStringFromClass([playerArray class]));

or, check with:

if ([playerArray isKindOfClass: [NSMutableArray class]])
{
...
}
like image 118
TomSwift Avatar answered Nov 17 '22 09:11

TomSwift


The error message indicates that playerArray is an instance of NSArray, not NSMutableArray. You cannot call removeObjectAtIndex on an instance of NSArray.

-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance
like image 35
Darren Avatar answered Nov 17 '22 07:11

Darren