-1

我有一個自定義類,它是SKSpriteNode的子類。我試圖覆蓋返回instancetypespriteNodeWithColor:size:方法。我試試這個:子類化方法返回實例類型

-(instancetype)initWithColor:(UIColor *)color size:(CGSize)size{ 
    self.color = color; 
    self.size = size; 

    //do other setup stuff here 
    return self; 
} 

它崩潰每次。在此先感謝您的幫助

+1

如果崩潰,報什麼錯誤?這個錯誤發生在哪裏? – ColinE

+0

錯誤是'線程1:EXC_BAD_ACCESS(代碼= 2,地址= 0x240)' – 68cherries

回答

4

你需要調用super

- (instancetype)initWithColor:(UIColor *)color size:(CGSize)size { 
    self = [super initWithColor:color size:size]; 

    if (self) { 
     // do other setup stuff here 
    } 

    return self; 
} 
+0

嗯....我以爲我曾嘗試過這一點。它現在有效,所以謝謝你提醒我。我想我的編程會話太長了。 – 68cherries

+0

是的,我剛剛做到了。我的意思是這樣做,但不得不出去。 – 68cherries

+0

如何子類化返回實例類型的類級方法之一? – GoldenJoe