本文介绍了无法删除 NSMutableArray 中的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我在运行应用程序时不断收到此错误:

For some reason, I keep getting this error when I run my app:

[__NSArrayI removeAllObjects]:发送到实例的无法识别的选择器

我像这样在 .m 中设置了 NSMutableArray:

I set up the NSMutableArray in .m like this:

@implementation ChooseViewController
{
    NSMutableArray *trackName;
}

并像这样填充它:

trackName = [JSON valueForKeyPath:@"results.trackName"];

但是当我运行这段代码时,它给了我错误:

But when I run this code, it gives me the error:

[trackName removeAllObjects];

其他一切正常,trackName 中的数据也正常.当我运行这段代码时,它只是一团糟.提到的唯一其他时间 trackName 数组是用于表视图计数:

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

你觉得有什么不对吗?我相信问题在于它认为它是一个 NSArray,但它们都不是.

Do you see anything wrong? I believe the problem is that somewhere it thinks it's an NSArray, but none of them are.

推荐答案

可变问题.

trackName = [JSON valueForKeyPath:@"results.trackName"];

这一行将导致对 trackName 的不可变数组.改成这样:

This line will result into immutable array to trackName. Change to this:

trackName = [[JSON valueForKeyPath:@"results.trackName"] mutableCopy];

现在,trackName 是可变的,以便您可以调用.

Now, trackName is mutable so that you can call.

[trackName removeAllObjects];

这篇关于无法删除 NSMutableArray 中的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 14:18