问题
Could anyone possibly help me here, why I am getting NSFastEnumerationMutationHandler crash all of a sudden in my code. I am all but blank why this crash poped up all of a sudden and how to squash this one.
Thanks.
回答1:
Crash Error: **** Terminating app due to uncaught exception 'NSGenericException', reason: '* **Collection <__NSArrayM: 0x610000859410> was mutated while being enumerated.'*
You must be trying to change an array while you using fast enumeration.
Example
for ( id anObject in anArray ) {
if ( /* anObject satisfies some condition */ ) {
[anArray removeObject:anObject];
}
}
That shouldn't be done. Use a different array or probably filteredArrayUsingPredicate: method to filter. Remedy, however, depends on what you're trying to do.
回答2:
Came here looking for a solution and ended up taking a copy of the original array to get around the issue.
for (NSObject *object in [array copy]) {
if(condition) {
[array removeObject....]
break;
}
}
来源:https://stackoverflow.com/questions/6608173/nsfastenumerationmutationhandler-crash