美文网首页程序员
enumerateObjectsUsingBlock的介绍

enumerateObjectsUsingBlock的介绍

作者: 谁先注册了阿K | 来源:发表于2018-07-17 10:35 被阅读0次

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]);
    }];

相关文章

网友评论

    本文标题:enumerateObjectsUsingBlock的介绍

    本文链接:https://www.haomeiwen.com/subject/uurwpftx.html