收集<__ NSArrayM:0x7fa1f2711910>在枚举时发生了变异

Shr*_*t K 6 objective-c uitableview nsmutablearray ios

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

但我可以将这个'NSGenericException'的解决方案联系起来,原因:Collection <__ NSArrayM:0x7fabb400>在 我的问题被枚举问题时发生了变异.因为在我添加时会发生这种终止,所以用数组删除字符串对象.

错误:

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

第一次抛出调用堆栈:

(

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
)
Run Code Online (Sandbox Code Playgroud)

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

我正在做的事情:

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

这是我的代码

@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);

            }

        }

    }
Run Code Online (Sandbox Code Playgroud)

das*_*ght 19

您不能删除NSMutableArray迭代它的项目.

有几种解决方案:

  • 迭代数组的副本

要么

  • 使用基于索引的for循环而不是for each语法.

不复制数组可以节省分配和几个CPU周期:

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