How to define initializers in a protocol extension?

后端 未结 4 483
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 20:06
protocol Car {
     var wheels : Int { get set}

     init(wheels: Int)

}

extension Car {
    init(wheels: Int) {
        self.wheels = wheels
    }
}
<
4条回答
  •  感情败类
    2021-01-30 21:02

    My understanding is that this isn't possible, because the protocol extension can't know which properties the conforming class or struct has - and therefore cannot guarantee they are correctly initialized.

    If there are ways to get around this, I'm very interested to know! :)

提交回复
热议问题