Collection <__NSArrayM: 0x7fa1f2711910> was mutated while being enumerated

后端 未结 5 1296
轮回少年
轮回少年 2020-12-30 14:05

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

but i can relate solution of this 'NSGenericException', reason: Collection <

5条回答
  •  半阙折子戏
    2020-12-30 14:20

    You cannot delete items from a NSMutableArray while iterating it.

    There are several solutions to this:

    • Iterate a copy of the array

    or

    • Use an index-based for loop instead of the for each syntax.

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

提交回复
热议问题