Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

You forgot block's return type:

NSSet* contents = [self.content objectsPassingTest:^BOOL(id obj, BOOL* stop) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top