IOS中使用block的错误

在ios应用中一个单例模式:

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

然后就得到这样的错误:

use of undeclared identifier self

doStuff是单例模式的方法。假如在其他方法中声明这个block,Xcode运行正常。

请各位前辈帮我解释一下原因,谢谢

在界面中定义block,然后在@implementation文件中实例化选中的方法:

@interface YourClass {
   void (^ myBlock)();
}

@implementation YourClass

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


@end

每个方法都作为隐藏参数自身传递,如果在方法中的block可以获取self,如果不在方法中block,self就不是变量,block就不能‘see it’。