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

后端 未结 5 1297
轮回少年
轮回少年 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:31

    In you tableview deselect method, you wrote:

    for (NSString *test in updatedLocalityArray)
    {
        if ([test isEqualToString:tableViewCell.textLabel.text])
        {
            [updatedLocalityArray removeObject:test];
            NSLog(@"%@ *****", updatedLocalityArray);
        }
    }
    

    In that code you are enumerating through updatedLocalityArray and if you find a match you are removing that object from same array, this logic is causing the issue here. For fixing that you need to create local copy and do the changes in it and assign it back to the original array after the enumeration.

    NSMutableArray *tempArray = [updatedLocalityArray mutableCopy];
    for (NSString *test in updatedLocalityArray)
    {
        if ([test isEqualToString:tableViewCell.textLabel.text])
        {
            [tempArray removeObject:test];
            NSLog(@"%@ *****", tempArray);
        }
    }
    updatedLocalityArray = tempArray;
    

提交回复
热议问题