In Swift, why subclass method cannot override the one, provided by protocol extension in superclass

前端 未结 3 605
野趣味
野趣味 2021-01-05 15:06

I know the title of this question is confusing but the weird behaviour is explained in the example below:

protocol Protocol {
    func method() -> String
         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-05 15:36

    Basically yes, if there is a superclass that conforms to the protocol but doesn't provide an implementation for it, the protocol extension's implementation will be available in subclasses, even if the sublcasses have implementation (however they have to be casted to the superclass's or to the protocol's type).

    However, if the superclass has an implementation of the protocol's method, then protocol's implementation won't be available from neither the superclass nor it's subclasses.

提交回复
热议问题