Fix collection <__NSArrayM:0xb550c30> was mutated while being enumerated

Source: Internet
Author: User

When the program appears this prompt, because you side of the convenience of 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 method is to define an identical array, to facilitate array A and then manipulate 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 information, found block convenience than for convenience of about 20% faster, this 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.

Fix collection <__NSArrayM:0xb550c30> was mutated while being enumerated

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.