Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective C block declarations conflict
    text
    copied!<p>I've encountered some really annoying issue about objective c blocks. Let's say, we have a code like this:</p> <pre><code>//Class1.h #import &lt;Foundation/Foundation.h&gt; typedef void (^Class1Callback)(BOOL bl); @interface Class1 : NSObject { } - (id)initWithCallback:(Class1Callback)callback; @end //Class2.h #import &lt;Foundation/Foundation.h&gt; typedef enum Class2Enum { Foo, Bar, FooBar } Class2Enum; typedef void (^Class2Callback)(Class2Enum ce); @interface Class2 : NSObject { } - (id)initWithCallback:(Class2Callback)callback; @end //main.c #import "Class1.h" #import "Class2.h" int main(int argc, char *argv[]) { Class1 *class1 = [[Class1 alloc] initWithCallback:^(BOOL bl){ }]; Class2 *class2 = [[Class2 alloc] initWithCallback:^(Class2Enum ce){ }]; } </code></pre> <p>In this code I'm getting an error "incompatible block pointer types initializing 'void (^)(enum Class2Enum)', expected 'Class1Callback'" in line</p> <pre><code>Class2 *class2 = [[Class2 alloc] initWithCallback:^(Class2Enum ce){ }]; </code></pre> <p>But when I just reverse imports in main.c, ie</p> <pre><code>#import "Class2.h" #import "Class1.h" </code></pre> <p>error changes its place and description - now it's in line</p> <pre><code>Class1 *class1 = [[Class1 alloc] initWithCallback:^(BOOL bl){ }]; </code></pre> <p>and description is "incompatible block pointer types initializing 'void (^)(BOOL)', expected 'Class2Callback'"</p> <p>Why does the compiler expect 'Class2Callback' when the method's declaration says that the block type is Class1Callback? I have no idea why it's happening and where I should seek and what I should read to find the answer. Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload