9

我发现了一个有趣的行为,它看起来像一个错误......

基于以下文章描述的行为:

https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94

http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future

SomeSuperclass当我添加而不是直接采用协议时,输出不是我所期望的。

protocol TheProtocol {
    func method1()
}

extension TheProtocol {
    func method1() {
        print("Called method1 from protocol extension")
    }
    func method2NotInProtocol() {
        print("Called method2NotInProtocol from protocol extension")
    }
}

// This is the difference - adding a superclass
class SomeSuperclass: TheProtocol {
}

// It works as expected when it simply adopts TheProtocol, but not when it inherits from a class that adopts the protocol
class MyClass: SomeSuperclass {
    func method1() {
        print("Called method1 from MyClass implementation")
    }
    func method2NotInProtocol() {
        print("Called method2NotInProtocol from MyClass implementation")
    }
}

let foo: TheProtocol = MyClass()
foo.method1()  // expect "Called method1 from MyClass implementation", got "Called method1 from protocol extension"
foo.method2NotInProtocol()  // Called method2NotInProtocol from protocol extension

你知道这是一个错误还是设计使然?一位同事建议混合继承和协议扩展可能无法按预期工作。我打算使用协议扩展来提供默认实现......如果我不能这样做,那么我将不得不标记它@objc并返回到可选协议。

4

3 回答 3

2

来自The Ghost of Swift Bugs Future的帖子,这里是帖子末尾提到的协议扩展的调度规则。

  1. 如果变量的推断类型是协议:
  2. 并且该方法在原始协议中定义然后调用运行时类型的实现,无论扩展中是否存在默认实现。
  3. 并且该方法未在原始协议中定义,则调用默认实现。
  4. ELSE IF 推断的变量类型是类型 THEN 调用类型的实现。

因此,在您的情况下,您是说 method1() 是在协议中定义的,并且已在子类中实现。但是您的超类正在采用协议,但它没有实现 method1() 并且子类只是从超类继承并且不直接采用协议。这就是为什么我认为这是您调用 foo.method1() 时的原因,它不会调用第 1 点和第 2 点所述的子类实现。

但是当你这样做时,

class SomeSuperclass: TheProtocol {
func method1(){
 print("super class implementation of method1()")}
}

class MyClass : SomeSuperclass {

override func method1() {
    print("Called method1 from MyClass implementation")
}

override func method2NotInProtocol() {
    print("Called method2NotInProtocol from MyClass implementation")
}
}

然后当你打电话时,

 let foo: TheProtocol = MyClass()
foo.method1()  // Called method1 from MyClass implementation
foo.method2NotInProtocol() 

那么这个错误(这似乎是一个错误)的解决方法是,您需要在超类中实现协议方法,然后您需要覆盖子类中的协议方法。高温高压

于 2016-01-18T07:48:14.653 回答
0

请检查以下代码:

import UIKit

protocol TheProtocol {
    func method1()
    func method2NotInProtocol()
}

extension NSObject {

    func method1() {
        print("Called method1 from protocol extension")
    }
    func method2NotInProtocol() {
        print("Called method2NotInProtocol from protocol extension")
    }
}

// This is the difference - adding a superclass
class SomeSuperclass :NSObject, TheProtocol {

    override func method1() {
        print("Called method1 from SomeSuperclass implementation")
    }

}

// It works as expected when it simply adopts TheProtocol, but not when it inherits from a class that adopts the protocol
class MyClass : SomeSuperclass {

    override func method1() {
        print("Called method1 from MyClass implementation")
    }

    override func method2NotInProtocol() {
        print("Called method2NotInProtocol from MyClass implementation")
    }
}

    let foo: TheProtocol = MyClass()
    foo.method1()  // expect "Called method1 from MyClass implementation", got "Called method1 from protocol extension"
    foo.method2NotInProtocol()  // Called method2NotInProtocol from protocol extension

不是向 TheProtocol 编写扩展,而是向抽象类(上面代码中的 NSObject)编写扩展。这按预期工作。

于 2016-01-18T06:08:00.720 回答
0

尚未考虑的一个选项是将您的协议拆分为几个较小的协议,以便超类不需要符合包含它不打算实现的方法的协议。然后子类可以单独订阅这些其他协议。

于 2018-11-23T07:31:59.630 回答