ios – 协议扩展和子类

前端之家收集整理的这篇文章主要介绍了ios – 协议扩展和子类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道为什么下面没有打印出我认为应该的东西.

/* 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#.6cm4oqaq1http://krakendev.io/blog/subclassing-can-suck-and-heres-why,但我无法弄清楚这一点.在采用该协议的实体的子类上是否不遵守扩展方法

解决方法

答案是Method Dispatch a Bug in Swift.

方法分派是编译器用于选择在调用方法时执行的实现的机制. 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中的方法调度有一个很棒的解释.

Here你可以阅读我在协议扩展中写的关于方法调度的简短介绍.

猜你在找的iOS相关文章