不兼容的块指针类型objectsPassingTest:

时间:2013-06-27 14:56:18

标签: ios objective-c objective-c-blocks nsset

我想得到过滤NS54:

NSSet* contents = [self.content objectsPassingTest:^(id obj, BOOL* stop){
        NSNumber* chapterNo = ((LTContent*)obj).chapterNo;
        return [chapterNo integerValue] < 0;
    }];

但此代码会触发错误:incompatible block pointer types sending 'int (^)(id, BOOL *)' to parameter of type 'BOOL (^)(id, BOOL *)

如果我改变代码:

NSSet* contents = [self.content objectsPassingTest:^(id obj, BOOL* stop){
        NSNumber* chapterNo = ((LTContent*)obj).chapterNo;
        BOOL a = [chapterNo integerValue] < 0;
        return a;
    }];

它完美无缺。但我不想使用奇数线。第一个片段有什么问题?

2 个答案:

答案 0 :(得分:4)

你忘记了区块的返回类型:

NSSet* contents = [self.content objectsPassingTest:^BOOL(id obj, BOOL* stop) {

答案 1 :(得分:3)

为块指定显式返回类型BOOL

NSSet* contents = [set objectsPassingTest:^BOOL(id obj, BOOL* stop) {
    // ...
    return [chapterNo integerValue] < 0;
}];

否则编译器从中派生返回类型 返回语句,在你的情况下是int