NSMutableArray禁止用for in枚举
NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
[array addObject:@"1"];
[array addObject:@"2"];
[array addObject:@"3"];
[array addObject:@"4"];
for (NSString *str in array){ //NSMutableArray禁止用for in枚举,系统会报错
if([str isEqualToString:@"3"]{
[array removeObject:str];
}
}
一种常用解决办法:
通过不可变数组的枚举来对MutableArray进行过滤操作
NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
[array addObject:@"1"];
[array addObject:@"2"];
[array addObject:@"3"];
[array addObject:@"4"];
NSArray *tempArray = [array copy];
for(NSString *str in tempArray){//tempArray是不可变数组能进行for in枚举
if([str isEqualTo:@"3"] || [str isEqualTo:@"4"]){
[array removeObject:str];//通过tempArray的枚举来对(MutableArray)array进行过滤操作
}
}
一种思路:通过倒序遍历来删除
enumerateObjectUsingBlock:^(){
}
网友评论