What are convenience required initializers in Swift?

后端 未结 3 1037
天涯浪人
天涯浪人 2020-12-31 05:11

I saw in this answer that the user specifies a convenience required init(). What exactly does this mean?

I understand that the required key

3条回答
  •  盖世英雄少女心
    2020-12-31 06:12

    Declaring required initialiser of class as convenience lets subclasses easily inherit it and thus ommit its implementation

    protocol P {
        var some: Int! {get}
        init(some: Int)
    }
    
    class C: P {
        private(set) var some: Int!
        convenience required init(some: Int) {
            self.init()
            self.some = some
        }
    }
    
    class D: C {
        // no need in required init(some: Int)...
    }
    

提交回复
热议问题