/* Fails */ protocol TheProtocol { func update() } class A: TheProtocol { } class B : A {} extension TheProtocol { func update() { print("Called update from TheProtocol") } } extension TheProtocol where Self: B { func update() { print("Called update from B") } } let instanceB = B() instanceB.update() let instanceBViaProtocol:TheProtocol = B() instanceBViaProtocol.update()
这将打印以下内容:
Called update from B Called update from TheProtocol // Why not: Called update from B (extension)
我特别想知道为什么
instanceBViaProtocol.update()
不执行TheProtocol上的扩展中的update():
extension TheProtocol where Self: B { func update() { print("Called update from B") } }
我认为既然B继承自采用TheProtocol的A,那么我认为B也会隐含地采用TheProtocol.
将协议采用从A移至B产生预期结果.
protocol TheProtocol { func update() } class A { // Remove TheProtocol } class B : A,TheProtocol {} // Add TheProtocol extension TheProtocol { func update() { print("Called update from TheProtocol") } } extension TheProtocol where Self: B { func update() { print("Called update from B") } } let instanceB = B() instanceB.update() let instanceBViaProtocol:TheProtocol = B() instanceBViaProtocol.update()
结果:
Called update from B Called update from B
我看了https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94#.6cm4oqaq1和http://krakendev.io/blog/subclassing-can-suck-and-heres-why,但我无法弄清楚这一点.在采用该协议的实体的子类上是否不遵守扩展方法?
解决方法
方法分派是编译器用于选择在调用方法时执行的实现的机制. Swift使用3种方法调度.你可以读一下here
调度方法由引用的类型决定,而不是由实例的类型决定.在您的情况下,引用类型是TheProtocol.
let instanceBViaProtocol:TheProtocol = B() instanceBViaProtocol.update()
您有一个协议扩展,它定义了需求方法的常见行为.在这种情况下,使用动态分派.这意味着应该使用在B中声明的实现.但是有a bug in Swift引起了这个问题.
对于每种类型,Swift使用见证表来注册用于动态分派的实现.该错误导致B类无法在TheProtocol的Witness表中注册其update()的实现.当通过TheProtocol表调度update时,使用了错误的实现.
这里有一些改变的例子.请注意,如果在超类中声明更新并在子类中覆盖它,它将按预期工作.这是看到这个错误的最明智的方法.
protocol TheProtocol { func update() } class A: TheProtocol { func update(){ print("Implementation of A") } } class B : A { override func update(){ print("Implementation of B") } } //All those who conform to TheProtocol will execute this. extension TheProtocol { func update() { print("Common: TheProtocol") } } extension TheProtocol where Self: B { func update() { print("Common: TheProtocol for B's") } } extension TheProtocol where Self: A { func update() { print("Common: TheProtocol for A's") } } let instanceBViaProtocol:TheProtocol = B() //It prints "Implementation of B" instanceBViaProtocol.update()
我希望这能回答你的问题.
https://www.raizlabs.com/dev/2016/12/swift-method-dispatch/对swift中的方法调度有一个很棒的解释.