gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:39:33 25 4
gpt4 key购买 nike

当我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com