NSArray遍历和修改崩溃

//一.代码

NSArray *array = [self.dataList mutableCopy]; 或   NSArray *array = [NSArray arrayWithArray:self.dataList];

                if (array && array.count > 0) {

                    for (ChatHeader *header in array){ //遍历

                        if (currConact.userid == header.curContact.userid){

                            RSLog(@"退出群组删除好友消息:%@",header);

                                self.dataList = [self.dbManager getLocalMessageChatHeader]; 重新获取数组的值

                                [self.tableView reloadData];

//二.报的错误
6] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x15f8e55a0> was mutated while being enumerated.'

*** First throw call stack:

(0x182175900 0x1817e3f80 0x182175334 0x10042efe4 0x100b6dbf0 0x100b6dbb0 0x100b7a6c8 0x100b718a0 0x100b6dbb0 0x100b7ce10 0x100b7c4d8 0x181ddd470 0x181ddd020)

libc++abi.dylib: terminating with uncaught exception of type NSException

三.解决方案 :NSArray = [[NSArray alloc]init] ,然后再赋值 array = [NSArray arrayWithArray:self.dataList];

原因: 在你遍历这个数组时,又同时修改了这个数组,所以会报错

将来的自己,会感谢现在不放弃的自己!
原文地址:https://www.cnblogs.com/TheYouth/p/5151079.html