2
votes

I have a block:

typedef id (^completionBlock)(id data, NSURLResponse *urlResponse, NSError *error);

And in a class method I try to populate this block with some code.

request.requestCompletedBlock = ^(id data, NSURLResponse *urlResponse, NSError *error){
  ...
return object;
};

requestCompletedBlock is of type completionBlock obviously.

I get the following error:

"Incompatible block pointer types assigning to 'completionBlock' (aka 'id (^)(_strong id, NSURLResponse *_strong, NSError *__strong)') from 'void *(^)(_strong id, NSURLResponse *_strong, NSError *__strong)'"

Obviously my syntax is wrong somewhere, but where?

Thanks very much,

Vb

3

3 Answers

7
votes

For whatever reason, the compiler is inferring that the return type of your inline block is void*, not id. You can force it to use a return type of id by putting the return type after the ^ like so:

request.requestCompletedBlock = ^id (id data, NSURLResponse *urlResponse, NSError *error) {
    //                           ~~
    //                        Return type
}

See this page for a detailed description of block syntax.

1
votes

The compiler is inferring "void*" for the type of "object". You can explicitly declare the return type of the literal like so:

^id(id data, ...) { ... }
1
votes

Your syntax is correct, it's the type of your data that you got wrong.

Your object is of type void*, but your block expects you to return id. This means that you should either wrap object into, say, NSData*,

request.requestCompletedBlock = ^(id data, NSURLResponse *urlResponse, NSError *error){
    ...
    return (id)[NSData dataWithBytes:object length:numBytes];
};

or change the declaration of the completionBlock to expect a return type of void*.