lionK
lionK

Reputation: 27

Incompatible block pointer type? Cannot return NSArray

I have a block we return an array in callback. Then I create another method to store that array as below:

- (NSArray *)getUserData{

[self fetchDataByUserId:self.userID completionHandler:^(NSArray *record) {
    return record;
}];
}

I received this kind of error so please help me, I don't understand.

"Incompatible block pointer types sending 'NSArray *(^)(NSArray * __strong) to parameter of type 'void (^)(NSArray *_strong)"

Upvotes: 0

Views: 353

Answers (2)

Huy Le
Huy Le

Reputation: 2513

Reason is explained by @user3386109, this is the right way.

- (void)getUserData:(void (^)(NSArray *record))complete {

    [self fetchDataByUserId:self.userID completionHandler:^(NSArray *record) {
        complete(record);
    }];

}

or

- (void)getUserData:(void (^)(NSArray *record))complete {

    [self fetchDataByUserId:self.userID completionHandler:complete];

}

Moreover, about the Error Message: "Incompatible block pointer type", you should google "How to use Block in objective-c", ex: http://rypress.com/tutorials/objective-c/blocks

Because your completionHanler is not a return type block, it's a void block.

(void (^)(NSArray *record)) is different with (NSArray * (^)(NSArray *record))

Upvotes: 2

user3386109
user3386109

Reputation: 34829

The completionHandler is not called immediately, and is not expected to return a value. You can think of the code in the completionHandler as a callback function. The caller is some framework code that executes whenever the fetch completes. The framework code calls your completionHandler to let you know that the fetch is finished, and doesn't expect any return value.

So the getUserData method should really be a startUserDataFetch method, and you need additional code to process the data if ever / whenever it actually arrives.

Upvotes: 1

Related Questions