Search code examples
objective-cinheritancesprite-kitskspritenodeinstancetype

Subclassing a Method Returning instancetype


I have a custom class which is a subclass of SKSpriteNode. I am trying to override the spriteNodeWithColor:size: method which returns instancetype. I try this:

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

     //do other setup stuff here
     return self;
}

but it crashes every time. Thanks in advance for your help


Solution

  • You need to call super:

    - (instancetype)initWithColor:(UIColor *)color size:(CGSize)size {
        self = [super initWithColor:color size:size];
    
        if (self) {
          // do other setup stuff here
        }
    
        return self;
    }