ios - enumerateObjectsUsingBlock 曾经返回任何 BOOL 值吗?

标签 ios enumeration objective-c-2.0

当我在 NSArray 上搜索 enumerateObjectsUsingBlock 时,偶然去了 NSSet Class Reference,它提供了相同的方法签名...... Doc 说

Executes a given Block using each object in the set.
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, BOOL *stop))block
block: The Block to apply to elements in the set.

obj:The element in the set.

stop:A reference to a Boolean value. The block can set the value to YES to stop further processing of the set. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

Block 返回一个 bool 值,指示 obj 是否通过了测试。

那是哪个返回的 bool 值?既然block被声明为返回void,并且“stop”参数只能设置为“YES”,那么它的失败测试消息怎么会传递到block之外呢?

在我的测试过程中,我将此方法分配给 BOOL,但编译器警告“使用不兼容类型 'void' 的表达式初始化 'BOOL'(又名 'signed char')”

有任何想法吗?

最佳答案

该 block 不返回 bool 值。该 block 被传递一个对 bool 值的引用。在 block 内部,它应该设置为 YES 以指示枚举应该停止。

    [items enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        if (obj == something){
            *stop = YES;
        }
    }];

documentation for NSArray并不表示该 block 返回一个 bool 值:

Executes a given block using each object in the array, starting with the first object and continuing through the array to the last object.



并且 block 签名表明它返回无效。
(void (^)(id obj, BOOL *stop))
第一个 void: ( void (^)(id obj, BOOL *stop)) 是 block 的返回类型。

关于ios - enumerateObjectsUsingBlock 曾经返回任何 BOOL 值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149748/

相关文章:

ios - 我打算在带有参数的 init 方法中调用默认的 init 方法

cocoa - 模态表和模态返回值

ios - 使用 XLPagerTabStrip 在 child 之间传递数据

ios - 在 swift 中将 observe .value 转换为 .childAdded

c - Boolean 是 C 中的内置枚举吗?

c - 非类枚举方程式的良好实践(左边的值?)

ios - Xcode:如何创建出现在另一个 View Controller 中的弹出 View Controller

ios - Deinit 未在 UIViewController 上调用,但 Dealloc 是

ios - 黑色 UITableViewCell DetailDisclosure 附件按钮?

c - 如何将字符串中的字符交换为枚举字符串?