ios - 尝试从数组中取出对象,反转它们,然后将它们放回原处

标签 ios objective-c arrays

正如标题所说,我在从数组中取出对象、翻转它们并将它们放回原位时遇到问题。下面是我目前拥有的以这个错误结尾的代码

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectsAtIndexes:]: unrecognized selector sent to instance

我想知道是否有人知道如何解决这个问题?以下是有关我如何设置它的更多信息:

对象“PEG”是一个显示“-0.6”、“4.36”的NSString

GlobalSortedArray 是一个数组,其中充满了包含 PEG 对象的字典

//Declare variables
NSMutableArray *negArray = [[NSMutableArray alloc]init];
NSMutableIndexSet *index = [[NSMutableIndexSet alloc]init];
int negcount = 0;
NSDictionary *forLoopDict;


for (forLoopDict in globalSortedArray)
      {
          if ([[forLoopDict objectForKey:@"PEG"] hasPrefix:@"-"])
          {
              [index addIndex:negcount];
          }
          negcount++;
      }


NSLog(@"%@", negArray);

//    Removes objects from main array. This is what seems to be messing up.
[globalSortedArray removeObjectsAtIndexes:index];

//    Reverses the array
NSArray* reversedArray = [[negArray reverseObjectEnumerator] allObjects];

//    insters them back into the main array
[globalSortedArray insertObjects:negArray atIndexes:0];

最佳答案

您正尝试从 globalSortedArray 中删除项目。它是一个 NSArray 并且不可变。

关于ios - 尝试从数组中取出对象,反转它们,然后将它们放回原处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907164/

相关文章:

ios - 尝试从另一个类访问时类中的对象为零

ios - 循环遍历 AnyObject 数组

ios - 将 JS MessageMedia 数据与核心数据一起使用

c - 一维数组传递给两个函数,只有一个函数输出正确的结果。为什么?

php如何序列化对象数组?

javascript - 在 ko.observableArray 模型绑定(bind)问题中向下移动项目

ios - Google Analytics 实时功能不起作用

iphone - @class @synthesize 可用性

iphone - UIButton buttonWithType :101, 他们会拒绝我的应用吗?

objective-c - 用手指触摸移动时防止 View 对象跳跃