iOS 字典遍历enumeratekeysandobjectsusingblock stop是干什么

最近用到字典遍历,数组遍历的有关东西用到enumeratekeysandobjectsusingblock 块枚举法来遍历起初不知道回调参数中的stop是干什么用的 后来了解到他的神奇功效

先看代码
 NSDictionary *dictM2 = @{@"1":@"one",@"2":@"two",@"3":@"three"};
    [dictM2 enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"%@:%@",key,obj);
//        *stop = YES;
    }];

我们在控制台打印输出

屏幕快照 2017-08-10 下午8.10.57.png

发现字典里面的元素已经遍历完全

再看下面:
 NSDictionary  *dictM2 = @{@"1":@"one",@"2":@"two",@"3":@"three"};
    [dictM2 enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"%@:%@",key,obj);
        *stop = YES;
    }];

看控制台打印

屏幕快照 2017-08-10 下午8.21.37.png
发现只是遍历了第一个键值对,说明当stop为yes时,就会停止遍历,所以我们可以在块中加入条件(即当某种条件下去停止遍历)。

如图:

屏幕快照 2017-08-10 下午8.41.53.png

我们看到当我们加入条件判断以后,在key为@“2”的时候停止了遍历

所以我们得出:在我们用enumeratekeysandobjectsusingblock对字典或者数组或者NSSet进行遍历时,如果里面元素过多时我们不需要遍历,此时我们可以用stop进行限制

既然说到遍历顺便说一下几种常用的遍历方法

  • 第一种就是我们常用的for循环遍历
//////////处理数组//////////
    NSArray *arrayM = @[@"1",@"2",@"3",@"4"];
    NSInteger arrayMCount = [arrayM count];
    for (int i = 0; i<arrayMCount; i++) {
        NSString *obj = arrayM[i];
        NSLog(@"%@",obj);
    }
    
    //////////处理字典//////////
    NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"};
    NSArray *dictKeysArray = [dictM allKeys];
    for (int i = 0; i<dictKeysArray.count; i++) {
        NSString *key = dictKeysArray[i];
        NSString *obj = [dictM objectForKey:key];
        NSLog(@"%@:%@",key,obj);
    }
    
    //////////处理集合//////////
    NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
    NSArray *setObjArray = [setM allObjects];
    for (int i = 0; i<setObjArray.count; i++) {
        NSString *obj = setObjArray[i];
        NSLog(@"%@",obj);
    }
    
    //////////反向遍历----降序遍历----以数组为例
    NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"];
    NSInteger arrayMCount2 = [arrayM2 count] - 1;
    
    for (NSInteger i = arrayMCount2; i>0; i--) {
        NSString *obj = arrayM2[i];
        NSLog(@"%@",obj);
    }
  • 第二种for....in 遍历
//////////处理数组//////////
    NSArray *arrayM = @[@"1",@"2",@"3",@"4"];
    for (id obj in arrayM) {
        NSLog(@"%@",obj);
    }
    
    //////////处理字典//////////
    NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"};
    for (id obj in dictM) {
        NSLog(@"%@",dictM[obj]);
    }
    
    //////////处理集合//////////
    NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
    for (id obj in setM) {
        NSLog(@"%@",obj);
    }
    
    //////////反向遍历----降序遍历----以数组为例
    NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"];
    for (id obj in [arrayM2 reverseObjectEnumerator]) {
        NSLog(@"%@",obj);
    }
  • 基于enumeratekeysandobjectsusingblock的遍历方式
//////////处理数组//////////
    NSArray *arrayM = @[@"1",@"2",@"3",@"4"];
    [arrayM enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%zd--%@",idx,obj);
    }];
    
    //////////处理字典//////////
    NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"};
    [dictM enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"%@:%@",key,obj);
    }];
    
    //////////处理集合//////////
    NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
    [setM enumerateObjectsUsingBlock:^(id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSLog(@"%@",obj);
    }];
    
    //////////反向遍历----降序遍历----以数组为例
    NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"];
    [arrayM2 enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%zd--%@",idx,obj);
    }];
  • 还有一种NSEnumerator不经常用就不列举了
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容