Why use required Initializers in Swift classes?

前端 未结 4 599
臣服心动
臣服心动 2020-12-04 10:40

I am trying to understand the use of the required keyword in Swift classes.

class SomeClass 
{
    required init() {
        // initializer impl         


        
4条回答
  •  误落风尘
    2020-12-04 11:27

    I want to draw an attention on another solution provided by Required, apart from Matt has given above.

    class superClass{
        var name: String
        required init(){
            // initializer implementation goes here
            self.name = "Untitled"
        }
    }
    class subClass: superClass {
        var neakName: String = "Subclass Untitled"
    
    }
    let instanceSubClass = subClass()
    instanceSubClass.name        //output: "Untitled"
    instanceSubClass.neakName    //output: "Subclass Untitled"
    

    As you can check in above example, I've declared required init() on superClass, init() initializer of superClass has inherited by default on subClass, So you able to create an instance of subClass let instanceSubClass = subClass().

    But, suppose you want to to add one designated initializer on subClass to assign run time value to stored property neakName. Of course you can add it, but that will result to no initializers from the superClass will be inherited to subClass, So if you will create an instance of subClass you will create through its own designated initializer as below.

    class superClass{
        var name: String
        init(){
            // initializer implementation goes here
            self.name = "Untitled"
        }
    }
    class subClass: superClass {
        var neakName: String = "Subclass Untitled"
        init(neakName: String) {
            self.neakName = neakName
        }
    }
    let instanceSubClass = subClass(neakName: "Bobby")
    instanceSubClass.name       //output: "Untitled"
    instanceSubClass.neakName   //output: "Bobby"
    

    Here above, you won't be able to create an instance of subClass by just subClass(), But if you want that every subclasses of superClass must have their own init() initializer to create direct instance by subClass(). Just place required keyword before init() on superClass, it will force you to add init() initializer on subClass too - as below.

    class superClass{
        var name: String
        required init(){
            // initializer implementation goes here
            self.name = "Untitled"
        }
    }
    class subClass: superClass {
        var neakName: String = "Subclass Untitled"
        init(neakName: String) {
            self.neakName = neakName
        }
    }    // Compiler error <------------ required `init()` must be provided by subClass.
    let instanceSubClass = subClass(neakName: "Bobby")
    instanceSubClass.name       //output: "Untitled"
    instanceSubClass.neakName   //output: "Bobby"  
    

    SO, use required keyword before initializer on superclass, when you want all subclasses must have been implemented required initializer of superclass.

提交回复
热议问题