Terminating app due to uncaught exception ' nsgenericexception ', Reason: ' * * * Collection <__NSArrayM:0x17542520> was mutated while being enumerated. '
It is not literally difficult to understand that the reason for crash is that we iterate over the array while modifying the array data. For example:
Nsmutablearray *originalarray = [Nsmutablearray arraywithobjects:@ "1", @ "2", @ "3", @ "3", @ "4", nil];
for (id obj in Originalarray) {
if ([obj isequaltostring:@ "3"]) {
[Originalarray Removeobject:obj]
}
}
It's natural that we'll think of copy an array, iterating through the copy array, and we'll do it in the original array if the conditions are met.
Nsmutablearray *duplicatearray = [Originalarray mutablecopy];
for (id obj in Duplicatearray) {
if ([OB isequaltostring:@ "3"]) {
[Originalarray Removeobject:obj];
}
}
Exception crash Nsgenericexception,nsarray was mutated while being enumerated