ios - 协议(protocol)扩展和子类

标签 ios swift protocols

我想知道为什么下面的内容没有打印出我认为应该打印的内容。

/* 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。 将协议(protocol)采用从 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 ,但我无法弄清楚这一点。采用该协议(protocol)的实体的子类是否不接受扩展方法?

最佳答案

答案是方法调度 + a Bug in Swift

方法分派(dispatch)是编译器用来在调用方法时选择要执行的实现的机制。 Swift 使用 3 种方法分派(dispatch)。你可以阅读它here

调度方法,是由引用的类型决定的,而不是由实例的类型决定的。在您的例子中,引用类型是 TheProtocol。

let instanceBViaProtocol:TheProtocol = B()
instanceBViaProtocol.update()

您有一个协议(protocol)扩展,它定义了需求方法的通用行为。在这种情况下,使用动态调度。这意味着应该使用 B 中声明的实现。但是有 a bug in Swift这导致了问题。

对于每种类型,Swift 使用见证表来注册用于动态调度的实现。该错误导致 B 类无法在 TheProtocol 的 Witness 表中注册其 update() 的实现。并且当通过 TheProtocol 表调度更新时,使用了错误的实现。

这里是您的示例,但进行了一些更改。请注意,如果您在父类(super class)中声明更新并在子类中覆盖它,它会按预期工作。这是查看错误的最清晰方法。

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你可以阅读我写的一篇关于协议(protocol)扩展中的方法分派(dispatch)的简短文章。

关于ios - 协议(protocol)扩展和子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40433686/

相关文章:

ios - 如何以编程方式将 View 固定到 Swift 中的选项卡栏?

swift - 尝试执行 segue 导致 "terminating with uncaught exception of type NSException"

http - 一个端口可以监听两种不同的协议(protocol)吗?

使用另一个项目数据库的 Swift cloudkit

google-chrome - Chrome 忽略了协议(protocol)处理程序调用

communication - 通信协议(protocol)的逆向工程

ios - 我如何处理 GPUImage 图像缓冲区,以便它们可用于 Tokbox 之类的东西?

ios - 如何在 UISplitViewController 中向 Root View Controller 添加新的 subview

ios - 如何交叉编译 GCC 以生成适用于 iOS 设备(arm、armv7)的 libgfortran?

json - 预期解码 Dictionary<String, Any> 但发现一个数组而不是嵌套容器