Search code examples
iphoneiosipadblock

iOS Blocks - use of undeclared identifier self


I am new to blocks. I am inside a singleton and I do this

void (^ myBlock)() = ^(){ [self doStuff]; };

I receive this error use of undeclared identifier self.

doStuff is a method inside the singleton.

but if this block is declared inside another method, Xcode is OK.

Why is that? thanks.


Solution

  • you can define the block in your interface and initialize in any of your methods (including initializers ) in your @implementation file like below:

    @interface YourClass {
       void (^ myBlock)();
    }
    
    @implementation YourClass
    
      - (void)yourMethod {
        myBlock = ^(){ [self doStuff]; };
      }
    
    
    @end