Does a swift subclass *always* have to call super.init()

前端 未结 4 962
暖寄归人
暖寄归人 2021-01-30 21:46

If I have a swift subclass that:

  1. doesn\'t need to access self
  2. doesn\'t need to access any properties on self

Do

相关标签:
4条回答
  • 2021-01-30 22:10

    I think you don't have to call super. I tried to simulate situation as follow:

    class Animal {
    }
    
    class Parrot: Animal {
    
        override init() {
            // Some code (super not called)
        }
    }
    

    However, i would recommend to always call super.init in real app, like you use to do in Objective-C.

    0 讨论(0)
  • 2021-01-30 22:25

    Yes the designated initializer need to delegate up, so it must call the super.init(). If you don't write the line, the compiler will do it for you.

    So you do not have to explicitly write super.init(...), but the subclass has to, even if only behind the curtain.

    But remember, in the first phase you need to set the properties in the subclass, then the super.init() must be called. In the second phase you may change all the inherited properties. I think super.init() is a perfect separator between phase 1 and phase 2.

    From the docs:

    Safety check 2

    A designated initializer must delegate up to a superclass initializer before assigning a value to an inherited property. If it doesn’t, the new value the designated initializer assigns will be overwritten by the superclass as part of its own initialization.

    0 讨论(0)
  • 2021-01-30 22:30

    No, you don't have to.

    Assume you have the following classes.

    class a {
        let name: String
    
        init() {
            name = "james"
        }
    }
    
    class b: a {
        let title: String
    
        override init() {
            title = "supervisor"
        }
    }
    

    If you instantiate a variable with

    let myVar = b()

    Then,

    • override init() in b will be called
    • then the init() in a will be called

    Even though you didn't explicitly call super.init().


    This has been confirmed by Chris Laettner on the swift-user's email list. It kicks in when your super class has a single designated initializer with a zero-argument init. This is why you don’t have to call super.init() when deriving from NSObject.

    *Thanks to Wingzero's comment below

    0 讨论(0)
  • 2021-01-30 22:31

    From the docs:

    Designated initializers must call a designated initializer from their immediate superclass.

    Also, regarding automatic initializer inheritance:

    Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:

    Rule 1 If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

    Rule 2 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.

    These rules apply even if your subclass adds further convenience initializers.

    So the answer to your question is as follows:

    Your subclass will always call a designated initializer of your superclass. If you don't write an initializer and the compiler doesn't complain, then it has used automatic initializer inheritance. And if you do write an initializer but it doesn't explicitly call a relevant upstream (or sidestream) initializer, it will be done for you automatically at the end of your initializer.

    Furthermore, the way chained initializers work is in a 2-phase process. In the first phase, it starts from the subclasses towards superclasses, assigning default values to any parameters. In the second phase, the process is run backwards, starting with superclasses and ending with your subclass, where the customization of parameters is done and overridden.

    This means that you must absolutely first in each init() set your variables, and then you're free to call (or not) the super.init() and run custom code. So as you asked above, if you want the super's init to run at the beginning, consider the 'beginning' to be just after your creating your variables:

    class a {
        var name: String
    
        init() {
            name = "james"
            println("a")
        }
    }
    
    class b: a {
        let title: String
    
        override init() {
            title = "supervisor"
            super.init()
            self.name = "john"
            println("b")
        }
    }
    
    let x = b()
    

    This will print a, then b.

    0 讨论(0)
提交回复
热议问题