I had a problem sending protocol methods.
I have a class hierarchy that looks like this:
protocol E { func test() } extension E { func test() { print("jello") } } class A: E { } class B: A { func test() { print("hello") } }
But when I call test on an instance of class B , statically forced to type A , "jello" is printed, not "hello".
let b: A = B() // prints "jello" not "hello" b.test()
My understanding is that the test "jello" print method "integrates" into instances of A (since A conforms to protocol E ). Then I provide another implementation of test inside B (which inherits form A ). I thought that polymorphism would work here, and calling test on a B instance that is stored inside A would print hello . What's going on here?
It works great when not using any protocol:
class A { func test() { print("jello") } } class B: A { override func test() { print("hello") } } let b: A = B()
How is it different from adopting a protocol that adds new methods to my parent class and provides a new implementation in a subclass, than wrote this method directly in the parent class and then overrides it in the subclass?
Do you guys have a workaround?
polymorphism ios xcode swift xcode7
Nicolas B.
source share