2

I am developing a SpriteKit game which uses a joystick. I have created a joystick SKNode Class.

The code for the class:

import Foundation
import SpriteKit

class Joystick: SKNode {
    var joystickPad: SKShapeNode!
    var joystickStick: SKShapeNode!

    var colour: UIColor!

    init(colour: UIColor) {
        //constructor method
        self.colour = colour;
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // lots of other code unrelated to the error
}

I got the following error when I run my code:

Super.init isn't called on all paths before returning from initializer

Can anyone please explain the error and tell me any solutions, I don't get why I'm getting that error, as I thought my class was just a class not a superclass.

Any Help would be much appreciated.

Additional Info:

I'm using:

  • Swift 4
  • Spritekit
  • Xcode 9.2
  • iPhone 6s Running the latest public release iOS
Anton Belousov
  • 1,140
  • 15
  • 34
Callum Williams
  • 121
  • 2
  • 9
  • 1
    Possible duplicate of [Super.init isn't called before returning from initializer](https://stackoverflow.com/q/30877156/1187415) – Martin R May 26 '18 at 16:21

2 Answers2

4

It's because your class inherited from SKNode.

Subclasses should call superclass init if they declares new designated initializer. You can find more info about initialization here.

You can just add super.init() call in your init to fix the error.

init(colour: UIColor) {
    //constructor method
    self.colour = colour;
    super.init()
}
Ivan Smetanin
  • 1,999
  • 2
  • 21
  • 28
0

as I thought my class was just a class not a superclass

Your class is a subclass. Therefore you must call the superclass's designated initializer in your initializer:

init(colour: UIColor) {
    self.colour = colour
    super.init()
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Here's my summary of the rules: http://www.apeth.com/swiftBook/ch04.html#_subclass_initializers – matt May 26 '18 at 16:03