ho trovato un comportamento interessante che sembra come un insetto ...Swift protocollo di estensione metodo di spedizione con superclasse e sottoclasse
Sulla base del comportamento descritto i seguenti articoli:
https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94
http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future
L'output non è quello che mi aspetto, quando aggiungo SomeSuperclass
anziché adottare direttamente il protocollo.
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
Sai se questo è un bug o di progettazione? Un collega ha suggerito che forse l'estensione dell'ereditarietà e delle estensioni del protocollo potrebbe non funzionare come previsto. Intendevo utilizzare l'estensione del protocollo per fornire un'implementazione predefinita ... se non riesco a farlo, allora dovrò purtroppo contrassegnarlo come @objc
e tornare a un protocollo opzionale.
Possibile duplicato di [Invio metodo di estensione protocollo in Swift 2.0] (http://stackoverflow.com/questions/32734403/protocol-extension-method-dispatch-in-swift-2-0) – mbelsky