3

I have a SKScene class in which I need to implement a custom initializer that override the initializer init(fileNamed: fileNamed) of the SKScene superclass SKNode, in order to do some proprietary initializations but keep the possibility offered by init(fileNamed: fileNamed) to load the scene from the interface builder.

I have some trouble to find the right syntax. I've tried the following:

class Try: SKScene
{
    override init(fileNamed: String)
    {
        super.init(fileNamed: fileNamed)
    }

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

But it returns the error : Initializer does not override a designated initializer from its superclass

It is however a designated initializer from a superclass, not the immediate superclass which is SKEffectNode, but the one above which is SKNode. How can I override the initializer in this case ?

J.

Jonah Begood
  • 317
  • 2
  • 14

1 Answers1

-2

From the SWIFT docs:

Conversely, if you write a subclass initializer that matches a superclass convenience initializer, that superclass convenience initializer can never be called directly by your subclass, as per the rules described above in Initializer Delegation for Class Types. Therefore, your subclass is not (strictly speaking) providing an override of the superclass initializer. As a result, you do not write the override modifier when providing a matching implementation of a superclass convenience initializer.

If I understand this right this code should work:

convenience init?(fileNamed filename: String)
{
    self.init(fileNamed: filename)
}
Stefan
  • 5,203
  • 8
  • 27
  • 51
  • 1
    Not working. It is looping indefinitely. I guess because the self.init is calling the convenience init declared above and not the one from the superclass. – Jonah Begood May 28 '16 at 09:57