zoukankan      html  css  js  c++  java
  • iOS之解决崩溃Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.

    崩溃提示:Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <CALayerArray: 0x14df0bd0> was mutated while being enumerated.'

    当程序出现这个提示的时候,是因为你一边便利数组,又同时修改这个数组里面的内容,导致崩溃,网上的方法如下:

    NSMutableArray * arrayTemp = xxx; 

        NSArray * array = [NSArray arrayWithArray: arrayTemp];  

        for (NSDictionary * dic in array) {        

            if (condition){            

                [arrayTemp removeObject:dic];

            }       

        }

    这种方法就是在定义一个一模一样的数组,便利数组A然后操作数组B

    今天终于找到了一个更快接的删除数组里面的内容以及修改数组里面的内容的方法:

    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);

            }

        }];

     

    利用block来操作,根据查阅资料,发现block便利比for便利快20%左右,这个的原理是这样的:

    找到符合的条件之后,暂停遍历,然后修改数组的内容

     

     

  • 相关阅读:
    hdu2844 Coins -----多重背包+二进制优化
    bzoj1452 [JSOI2009]Count ——二维树状数组
    cf685 div2 abcde
    cf675 div2 abcd
    cf669 div2 abcd
    cf668 div2 abcd
    UVA-10795
    cf665 div2 abcd
    Colored Cubes UVALive
    Image Is Everything UVALive
  • 原文地址:https://www.cnblogs.com/rglmuselily/p/6249015.html
Copyright © 2011-2022 走看看