關於error:Cannot assign to 'self' outside of a method in the init family

 有時候我們重寫父類的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];

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章