iOS数组异常:NSGenericException,Collection <__NSArrayM: 0x61800024f7b0> was mutated while being enumerated.'

崩溃日志:*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x61800024f7b0> was mutated while being enumerated.'
这是由于你一边便利数组,又同时修改这个数组里面的内容,导致崩溃,
解决方法如下
方法一

NSMutableArray * tempArr = xxx; 

    NSArray * array = [NSArray arrayWithArray: tempArr];  

    for (NSDictionary * dic in array) {        

        if (OK){            

            [tempArr removeObject:dic];

        }       

    }

方法一就是重新创建一个和你便利的数组一模一样临时数组,便利这个临时数组,来修改你自己的所便利的原数组
方法二

NSMutableArray *tempArr = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

    [tempArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        if ([obj isEqualToString:@"3"]) {

           *stop = YES;

            if (*stop == YES) {

                [tempArr replaceObjectAtIndex:idx withObject:@"想改啥"];

            }

        }

        if (*stop) {

            NSLog(@"array is %@",tempArr);

        }

    }];

block 便利比for便利效率高,因为for便利,不管是否完成修改操作,还是要把所有的数组便利一遍,而block便利遇到满足的条件就停止便利,执行修改操作,推荐使用block哦

你可能感兴趣的:(iOS数组异常:NSGenericException,Collection <__NSArrayM: 0x61800024f7b0> was mutated while being enumerated.')