When the program appears, it is because you are iterating through the array and simultaneously modifying the contents of the array, causing the crash

Source: Internet
Author: User

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 = [[NSMutableArrayalloc]initWithObjects:@"12",@"23",@"34",@"45",@"56"nil];[tempArray enumerateObjectsUsingBlock:^(idobj, NSUIntegeridx, 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.