问题
For some reason, I keep getting this error when I run my app:
[__NSArrayI removeAllObjects]: unrecognized selector sent to instance
I set up the NSMutableArray in .m like this:
@implementation ChooseViewController
{
NSMutableArray *trackName;
}
And populate it like this:
trackName = [JSON valueForKeyPath:@"results.trackName"];
But when I run this code, it gives me the error:
[trackName removeAllObjects];
Everything else works fine and the data in trackName works. It's just messing up when I run this code. The only other time trackName array is mentioned is for table view count:
return [trackName count];
Do you see anything wrong? I believe the problem is that somewhere it thinks it's an NSArray, but none of them are.
回答1:
Mutable problem.
trackName = [JSON valueForKeyPath:@"results.trackName"];
This line will result into immutable array to trackName. Change to this:
trackName = [[JSON valueForKeyPath:@"results.trackName"] mutableCopy];
Now, trackName
is mutable so that you can call.
[trackName removeAllObjects];
来源:https://stackoverflow.com/questions/29980036/cant-remove-all-objects-in-nsmutablearray