enumerateObjectsUsingBlock的介绍

enumerateObjectsUsingBlock的介绍
简单介绍一下他的相关性质,用法不难

第一简单说明:

Executes a given block using each object in the array, starting with the first object and continuing through the array to the last object. This method executes synchronously. Values allocated within the block are deallocated after the block is executed.

遍历是同步的 ,从第一个,到最后一个 ,block里面的对象在执行之后会被释放

第二参数介绍:

stop
A reference to a Boolean value. Setting the value to YES within the block stops further enumeration of the array. If a block stops further enumeration, that block continues to run until it’s finished.

当设置参数stop为yes时 遍历则会被终止, 即继续执行 ,否则,则会继续遍历 直至遍历结束.
*stop = true;

第三遍历时,会开启新的线程吗?

不会自动开启新的线程,若开发者 默认将代码写在主线程,则在主线程进行遍历, 写在子线程,则在子线程遍历

这个方法用来访问字典的,同数组的访问差不多.

  • (void)enumerateKeysAndObjectsUsingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block;

最后附上一段代码,直观

NSMutableArray * arr = [NSMutableArray array];
    for (int i= 0; i<1000; i++) {
        [arr addObject:@(i)];
    }
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx ==100) {
            *stop = true;
        }
        NSLog(@"index:%lu, currThe:%@",(unsigned long)idx,[NSThread currentThread]);
    }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,196评论 0 10
  • icons 使用:
    KongBF阅读 3,935评论 0 0
  • 重庆和湖南相邻,对于一个吃货来说,重庆火锅简直是天堂,由于近的原因,我选择了端午节去重庆旅游。 一次和高中好友的电...
    是不是个小仙女呀阅读 2,256评论 0 0
  • 小小寰球,有几个苍蝇碰壁。 嗡嗡叫,几声凄厉,几声抽泣。 蚂蚁缘槐夸大国,蚍蜉撼树谈何易。 正西风落叶下长安...
    竹叶青衣浮生红袍阅读 6,752评论 0 1
  • 林 儿 我和林儿相识已经16年了。16年前,我27岁,是一家校园报的总编辑,林儿大约18岁,在一家公司的总经理室任...
    秦占勇阅读 4,132评论 0 1

友情链接更多精彩内容