use*_*806 5 protocols ios swift
我想知道为什么以下内容没有打印出我认为应该的内容。
/* 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()
Run Code Online (Sandbox Code Playgroud)
这将打印以下内容:
Called update from B
Called update from TheProtocol // Why not: Called update from B (extension)
Run Code Online (Sandbox Code Playgroud)
我特别想知道为什么
instanceBViaProtocol.update()
Run Code Online (Sandbox Code Playgroud)
不在 TheProtocol 的扩展中执行 update():
extension TheProtocol where Self: B {
func update() {
print("Called update from B")
}
}
Run Code Online (Sandbox Code Playgroud)
我认为它会因为 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()
Run Code Online (Sandbox Code Playgroud)
结果:
Called update from B
Called update from B
Run Code Online (Sandbox Code Playgroud)
我看了一下https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94#.6cm4oqaq1和http://krakendev.io/blog/subclassing-can- Suck-and-heres-why,但我无法弄清楚这一点。采用该协议的实体的子类是否不支持扩展方法?
答案是Method Dispatch + Swift 中的一个 Bug。
方法分派是编译器在调用方法时选择要执行的实现的机制。Swift 使用 3 种方法调度。你可以在这里读到它
调度方法由引用的类型决定,而不是由实例的类型决定。在您的情况下,引用类型是 TheProtocol。
let instanceBViaProtocol:TheProtocol = B()
instanceBViaProtocol.update()
Run Code Online (Sandbox Code Playgroud)
您有一个协议扩展,它定义了需求方法的常见行为。在这种情况下,使用动态调度。这意味着应该使用 B 中声明的实现。但Swift 中有一个错误导致了这个问题。
对于每种类型,Swift 使用见证表来注册用于动态调度的实现。该错误导致 B 类无法在 TheProtocol 的 Witness 表中注册其 update() 的实现。当通过 TheProtocol 表调度更新时,使用了错误的实现。
这里有您的示例,但进行了一些更改。请注意,如果您在超类中声明 update 并在子类中重写它,它将按预期工作。这是查看 bug 的最清晰方法。
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()
Run Code Online (Sandbox Code Playgroud)
我希望这能回答你的问题。
https://www.raizlabs.com/dev/2016/12/swift-method-dispatch/对 swift 中的方法调度有一个很棒的解释。
在这里您可以阅读我写的关于协议扩展中的方法分派的简短文章。
| 归档时间: |
|
| 查看次数: |
706 次 |
| 最近记录: |