您的位置:首页 > 移动开发 > Objective-C

<转>for in、经典for循环和EnumerateObjectsUsingBlock

2017-06-22 18:32 417 查看
一直以为for循环和 for in 是一样的,例如:

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
NSMutableArray * arr = @[@1,@2,@3,@4,@5].mutableCopy;
for (NSNumber *obj in arr) {
NSLog(@"%@",obj);
}
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"for in cost: %0.3f",end - start);

CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < arr.count; i ++) {
NSLog(@"%@",arr[i]);
}
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
NSLog(@"for cost: %0.3f",endTime - startTime);


屏幕快照 1.png

但是偶然间写了这么一段就崩溃了

for (NSString * obj in arr) {
if ([obj integerValue] == 2) {
[arr addObject:@"6"];
}
}
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"for in cost: %0.3f Arr:%@",end - start,arr);


2FB1AD54-CC19-4FC0-BEF0-30B718922D60.png

但是如果写成for循环就没有问题,然后就去百度了一下发现:如果在for in 循环里,对这个数组进行了修改的话,无论是增,删,修改数组元素位置,都会扔一个异常出来,枚举的过程中数组发生了突变(<__NSArrayM: 0xa4fc000> was mutated while being enumerated.)

for in实际上是快速枚举,跟for循环意义上还是有区别的。NSArray的枚举操作中有一条需要注意:对于可变数组进行枚举操作时,不能通过添加或删除对象等这类操作来改变数组容器,否则就会报错.而本身这种操作也是有问题的,数组容器已经改变,可能遍历到没有分配的位置,用for循环机器不能自己察觉,但是枚举器可以察觉。

enumerateKeysAndObjectsUsingBlock用法

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"obj1",@"key1",@"obj2",@"key2", nil];
[dic enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
NSLog(@"value for key %@ is %@ ", key, value);
if ([@"key2" isEqualToString:key]) {
*stop = YES;
}
}];


NSDictionary有一个方法叫enumerateKeysAndObjectsUsingBlock,它就一个参数就是block,这个block携带了三个参数,这将要把dictionary里面的key和value每次一组传递到block,enumerateKeysAndObjectsUsingBlock会遍历dictionary并把里面所有的key和value一组一组的展示给你,每组都会执行这个block。这其实就是传递一个block到另一个方法,在这个例子里它会带着特定参数被反复调用,直到找到一个key2的key,然后就会通过重新赋值那个BOOL *stop来停止运行,停止遍历同时停止调用block。

更详细的用法

(http://blog.itpub.net/12231606/viewspace-1084119/)

enumerateObjectsWithOptions使用

NSArray *array = [[NSArray alloc]initWithObjects:@”a”,@”b”,@”c”,@”d”,@”e”,@”f”, nil];

//遍历数组元素
[array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"obj=%@   idx=%ld",obj,idx);
}];

//如果指定了NSEnumerationConcurrent顺序,那么底层通过GCD来处理并发执行事宜,具体实现可能会用到dispatch group。也就是说,这个会用多线程来并发实现,并不保证按照顺序执行

//NSEnumerationReverse 倒序排列
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"idx=%ld, id=%@", idx, obj);

//当需要结束循环的时候,调用stop,赋予YES
if (idx ==3) {
*stop = YES;
}

}];
//NSIndexSet类代表一个不可变的独特的无符号整数的集合,称为索引,因为使用它们的方式。这个集合被称为索引集    唯一的,有序的,无符号整数的集合
[NSIndexSet indexSetWithIndex:1];//创建一个索引集合,根据索引值
[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,8)];//创建一个索引集合,根据一个NSRange对象

[array enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,3)] options:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"\n\n\nidx=%ld, id=%@", idx, obj);
}];


for in、经典for循环和EnumerateObjectsUsingBlock 的比较

对于集合中对象数很多的情况下,for in 的遍历速度非常之快,但小规模的遍历并不明显(还没普通for循环快)

Value查询index的时候, 面对大量的数组推荐使用 enumerateObjectsWithOptions的并行方法.

遍历字典类型的时候, 推荐使用enumerateKeysAndObjectsUsingBlock,block版本的字典遍历可以同时取key和value(forin只能取key再手动取value)

参考文章:

(http://www.jianshu.com/p/ef3f1731a353)

(http://blog.sunnyxx.com/2014/04/30/ios_iterator/)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: