iOS-Bug: Collection <__NSArrayM: 0x> was mutated while being enumerated.

//init a NSArray

NSMutableArray*arr2= [NSMutableArrayarray];

for(inti=0; i<10; ++i) {

NSMutableDictionary*d = [NSMutableDictionarydictionaryWithObjectsAndKeys:[NSStringstringWithFormat:@"%d",i],@"a",nilnil];

[arr2addObject:d];

}

//Wrong

for(NSMutableDictionary*s in arr2) {

if([s[@"a"]isEqualToString:@"3"]) {

[arr2removeObject:s];//while here is safe for s[@"a"] = @"eeee";

}

}

//Safe 1.

[arr2enumerateObjectsUsingBlock:^(NSMutableDictionary*obj, NSUInteger idx,BOOLBOOL*stop) {

if([obj[@"a"]isEqualToString:@"3"]) {

*stop =YES;

[arr2removeObject:obj];// or obj[@"a"] = @"4ewewewe";

}

}];

//Safe 2.

NSArray*tmp = [NSArrayarrayWithArray:arr2];

for(NSMutableDictionary*s in tmp) {

if([s[@"a"]isEqualToString:@"3"]) {

s[@"a"] =@"eeee";

[arr2removeObject:s];

}

}

PS: 搜一句补充的话

//forin 循环中的便利内容不能被改变, 是因为如果改变其便利的内容会少一个, 而系统是不会允许这个发生的所以就会crash...但是当改变最后一个的内容时, 就不会crash, 是因为此时遍历已经结束, 结束之后对内容做修改是允许的。

你可能感兴趣的:(iOS-Bug: Collection <__NSArrayM: 0x> was mutated while being enumerated.)