![关于error:Cannot assign to 'self' outside of a method in the init family 关于error:Cannot assign to 'self' outside of a method in the init family](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写。在这种方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息例如以下:error:Cannot assign to 'self' outside of a method in the init family
原因:仅仅能在init方法中给self赋值。Xcode推断是否为init方法规则:方法返回id。而且名字以init+大写字母开头+其它 为准则。比如:- (id) initWithXXX;
出错代码:- (id) Myinit{
self = [super init];
……
}
解决方法:- (id) initWithMy
{
self = [super init];
}