Search code examples
iosobjective-cobjective-c-blocksnsset

incompatible block pointer types objectsPassingTest:


I want to get filtred NSSet:

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

But this code fires an error: incompatible block pointer types sending 'int (^)(id, BOOL *)' to parameter of type 'BOOL (^)(id, BOOL *)

If I change code:

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

it works perfect. But I don't want to use odd line. What's wrong in first snippet?


Solution

  • Specify the explicit return type BOOL for the block:

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

    Otherwise the compiler derives the return type from the return statement, and that is int in your case.