集合< __ NSArrayM:0x7fa1f2711910>被枚举时发生了变异 [英] Collection <__NSArrayM: 0x7fa1f2711910> was mutated while being enumerated

查看:109
本文介绍了集合< __ NSArrayM:0x7fa1f2711910>被枚举时发生了变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多标题相似的问题,但没有一个对我有帮助.

There are number of questions with similar title but none them of helped me.

但是我可以关联此'NSGenericException'的解决方案,原因:集合< __ NSArrayM:0x7fabb400>被枚举时发生了变异 我的问题. 因为这种终止正在发生 当我添加时,使用数组删除字符串对象.

but i can relate solution of this 'NSGenericException', reason: Collection <__NSArrayM: 0x7fabb400> was mutated while being enumerated question with my question. Because this termination is happening when I'm adding,removing string object with array.

错误:

由于未捕获的异常"NSGenericException"而终止应用程序, 原因:"***集合< __ NSArrayM:0x7fa1f2711910>在枚举时发生了突变."

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x7fa1f2711910> was mutated while being enumerated.'

第一个引发调用堆栈:

(

0   CoreFoundation                      0x00000001087b8c65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000108422bb7 objc_exception_throw + 45
2   CoreFoundation                      0x00000001087b85c4 __NSFastEnumerationMutationHandler + 132
3                                       0x0000000107e24cf5 -[ProfessionalRegSecond tableView:didDeselectRowAtIndexPath:] + 773
4   UIKit                               0x0000000109759d71 -[UITableView _deselectRowAtIndexPath:animated:notifyDelegate:] + 347
5   UIKit                               0x000000010974deea -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 187
6   UIKit                               0x000000010968062c _applyBlockToCFArrayCopiedToStack + 314
7   UIKit                               0x00000001096804a6 _afterCACommitHandler + 533
8   CoreFoundation                      0x00000001086ebca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
9   CoreFoundation                      0x00000001086ebc00 __CFRunLoopDoObservers + 368
10  CoreFoundation                      0x00000001086e1a33 __CFRunLoopRun + 1123
11  CoreFoundation                      0x00000001086e1366 CFRunLoopRunSpecific + 470
12  GraphicsServices                    0x000000010d6c8a3e GSEventRunModal + 161
13  UIKit                               0x000000010965c900 UIApplicationMain + 1282
14                                      0x0000000107de14cf main + 111
15  libdyld.dylib                       0x000000010b585145 start + 1
)

libc ++ abi.dylib:以类型为NSException的未捕获异常终止

libc++abi.dylib: terminating with uncaught exception of type NSException

我要做什么:

我需要数组,当我选择单元格时,我想将该对象添加到数组&中.当我取消选择行时,我想从数组中删除该行,因此结果数组将仅是选定的条目.该怎么做?

I need array in such way that when i select cell i want to add that object to array & when I'm deselecting row i want to remove that row from array so as a result array will be only selected entries. How to do this ?

这是我的代码

@implementation Professional

    {
         NSMutableArray *updatedLocalityArray; // alloc,init did in viewDidLoad
         NSString *toCheck;
    }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

   {

        UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];

        NSLog(@"%@",tableViewCell.textLabel.text);

        toCheck = tableViewCell.textLabel.text;
        [updatedLocalityArray addObject:toCheck];
        NSLog(@"%@ *****", updatedLocalityArray);

   }

  - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
           NSLog(@"%@",tableViewCell.textLabel.text);

        for (NSString *test in updatedLocalityArray)
        {
            if ([test isEqualToString:tableViewCell.textLabel.text])
            {

                [updatedLocalityArray removeObject:test];
                NSLog(@"%@ *****", updatedLocalityArray);

            }

        }

    }

推荐答案

您不能在NSMutableArray进行迭代时删除项目.

You cannot delete items from a NSMutableArray while iterating it.

对此有几种解决方案:

  • 迭代数组的副本

  • 使用基于索引的for循环代替for each语法.
  • Use an index-based for loop instead of the for each syntax.

不复制阵列可节省您的分配时间和几个CPU周期:

Not copying the array saves you an allocation and a few CPU cycles:

for (int i = updatedLocalityArray.count-1 ; i >= 0 ; i--)
{
    NSString *test = updatedLocalityArray[i];
    if ([test isEqualToString:tableViewCell.textLabel.text])
    {
        [updatedLocalityArray removeObjectAtIndex:i];
        NSLog(@"%@ *****", updatedLocalityArray);
    }
}

这篇关于集合&lt; __ NSArrayM:0x7fa1f2711910&gt;被枚举时发生了变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆