-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