4
votes

Tried many variations of the derivation below but none work.

import Foundation
import SceneKit


class test:SCNScene{
    override init(){
        super.init()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }


    convenience init? (i:UIViewController){
        self.init(named:"") //Compile Error:use of self delegating initializer before self.init is called
    }

}

According to Swift documentation, rule 2 of initialization, shouldn't init?(named:String) convenience failable Initializer be available after implement the 2 designated Initializer? What am i getting wrong?

1
Rule 2 states "A convenience initializer must call another initializer from the same class." - your class doesn't define init?(named:String) - Paulw11
The rule i was referring is on "Automatic Initializer Inheritance" topic: "If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers." - MiguelSlv
Yes, the superclass initialisers are available to you but that doesn't mean you can ignore the rule I quoted - you must call a non-convenience initialiser from your class - Paulw11
ok, so how to implement init?(named:String) at test class? init?(named:String) is a convenience initializer and there is no designated initializer that accepts the file not even there is a way to load after self.init() - MiguelSlv
You need to call self.init() before your call to self.init(named:"") as the error message says - Paulw11

1 Answers

8
votes

The initialiser delegation rule #2 states

A convenience initializer must call another initializer from the same class.

Your class doesn't define init?(named:String), so it will call superclass initialiser (which you do have access to under the other rule #2 you were referring to), but this won't satisfy the requirement to call a non-convenience initialiser from your class.

You can simply call self.init before calling the superclass initialiser.

convenience init? (i:UIViewController){
        self.init()
        self.init(named:"") 
}