Error message:
Cause of Error:
Crashes when the program performs a traversal and modifies the contents of the array at the same time.
Here's how to fix it:
Method 1: define an identical array, iterate over the new array, and modify the original array.
For example:
Nsmutablearray *arraytemp = [@[@ "AA", @ "VV", @ "BB"] mutablecopy]; Nsarray *array = [Nsarray arraywitharray:arraytemp]; for (NSString * str in array) { if (condition) { [arraytemp removeobject:str]; }}
Method 2:
Using an extended method of Enumerateobjectsusingblock in the array, use block syntax
-(void) Enumerateobjectswithoptions: (nsenumerationoptions) opts usingblock: (void (^) (id obj, Nsuinteger idx, BOOL *stop);
The code is as follows:
Create an Array object Nsmutablearray *array =[@[@ "AA", @ "CC", @ "BB", @ "DD"] mutablecopy]; Call this method [array Enumerateobjectsusingblock: ^ (id obj, Nsuinteger idx, BOOL *stop) { //To determine if the element you want to manipulate is the same, Same return Yes if ([obj isequaltostring:@ "CC"]) { *stop = yes; if (*stop = = yes) {//If yes, delete, or modify the operation. [Array replaceobjectatindex:idx withobject:@ "CCC"]; [Array removeobject:obj]; } } if (*stop) {//operation succeeded. Output Array NSLog (@ "%@", array); } ];
Learning iOS Path: object-c error Collection <__NSArrayM:0xb550c30> was mutated while being enumerated.