What does the “required” keyword in Swift mean?

后端 未结 3 1655
暖寄归人
暖寄归人 2020-12-08 20:45

Take the following example:

class A {
    var num: Int

    required init(num: Int) {
        self.num = num
    }
}

class B: A {
    func haveFun() {
              


        
3条回答
  •  难免孤独
    2020-12-08 21:03

    “The use of the required modifier ensures that you provide an explicit or inherited implementation of the initializer requirement on all subclasses of the conforming class, such that they also conform to the protocol.”

    Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2).” iBooks. https://itun.es/us/jEUH0.l

提交回复
热议问题