2014-12-17 16:22:15.838 Precious Metals trends [4638:150754] * * * terminating app due to uncaught exception ' nsgenericexception ', Reason: ' * * * Collection <__NSCFArray:0x799dba00> was mutated while being enumerated. '
When the program appears, it is because you walk through the array, but also modify the contents of the array, resulting in a crash, the online method is as follows:
123456789 |
nsmutablearray * arraytemp = xxx; nsarray * array = [ nsarray arraywitharray: arraytemp] ; for ( Nsdictionary * dic in array) { &NBSP;&NBSP;&NBSP; if (condition) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; [arraytemp removeobject:dic]; &NBSP;&NBSP;&NBSP; } } |
This approach is to define an identical array, iterate over the array A and then manipulate the array b
Today we finally found a faster way to delete the contents of the array and modify the contents of the array:
1234567891011121314151617181920212223 |
NSMutableArray
*tempArray = [[
NSMutableArray
alloc]initWithObjects:
@"12"
,
@"23"
,
@"34"
,
@"45"
,
@"56"
,
nil
];
[tempArray enumerateObjectsUsingBlock:^(
id
obj,
NSUInteger
idx,
BOOL
*stop) {
if
([obj isEqualToString:
@"34"
]) {
*stop =
YES
;
if
(*stop ==
YES
) {
[tempArray replaceObjectAtIndex:idx withObject:
@"3333333"
];
}
}
if
(*stop) {
NSLog
(
@"array is %@"
,tempArray);
}
}];
|
Using block to operate, according to the data, it is found that block traversal is about 20% faster than for traversal, the principle is this:
After a qualifying condition is found, the traversal is paused and the contents of the array are modified
This method is very simple.
When the program appears, it is because you are iterating through the array and simultaneously modifying the contents of the array, causing the crash