Can't remove all objects in NSMutableArray

余生长醉 提交于 2019-12-11 17:04:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!