iOS Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NS...

报错信息:
Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x6000021f9d40> was mutated while being enumerated.'

原因:
数组在遍历的时候被改变。(苹果不允许数组在遍历的时候改变数组的元素)

解决:
(1)创建一个与原可变数组一样的数组
(2)遍历第一步得到的新数组
(3)对原可变数组进行操作即可

代码如下:

NSArray *array = [NSArray arrayWithArray:_muArr];
    for (NSTimer *timer in array) {
        [timer invalidate];
        [_muArr removeObject:timer];
    }
    _muArr = nil;

你可能感兴趣的:(iOS Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NS...)