Problems with block syntax in Objective-C - typedef block with return type and arguments

objective-c, objective-c-blocks

Solution

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.

Problem

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

Original source