子类化返回instancetype的方法

67c*_*ies -1 inheritance objective-c instancetype sprite-kit skspritenode

我有一个自定义类,它是一个子类SKSpriteNode.我试图覆盖spriteNodeWithColor:size:返回的方法instancetype.我试试这个:

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

     //do other setup stuff here
     return self;
}
Run Code Online (Sandbox Code Playgroud)

它每次都会崩溃.在此先感谢您的帮助

Mar*_*bri 5

你需要打电话super:

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

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

    return self;
}
Run Code Online (Sandbox Code Playgroud)