From the โPhantom of the fast mistakes of the futureโ message, here are the sending rules for protocol extensions that are mentioned at the end of the message.
- IF the selected variable type is a protocol:
- And the method is defined in the original THEN protocol. implementation of run-time types, regardless of whether the extension has a default implementation.
- And the method is not defined in the original protocol, then the default implementation is called.
- ELSE If the derived variable type is a THEN type, the type implementation is called.
So, in your state, you say that method1 () is defined in the protocol and implemented in a subclass. But your superclass uses the protocol, but does not implement the 1 () method, and the subclass simply inherits from the superclass and does not directly accept the protocols. Therefore, I believe that this is the reason you call foo.method1 (), it does not call the implementation of the subclass, as described in paragraphs 1 and 2.
But when you do it,
class SomeSuperclass: TheProtocol { func method1(){ print("super class implementation of method1()")} } class MyClass : SomeSuperclass { override func method1() { print("Called method1 from MyClass implementation") } override func method2NotInProtocol() { print("Called method2NotInProtocol from MyClass implementation") } }
and then when you call
let foo: TheProtocol = MyClass() foo.method1()
So what could be a workaround for this error (which seems to be an error) is that you need to implement the protocol method in the superclass, and then you need to override the protocol method in the subclass. NTN
iamyogish
source share