Swift 2-Protokollerweiterung ruft überschriebene Methode nicht korrekt auf

Ich habe ein Problem mit den Protokollerweiterungen von Swift 2 mit Standardimplementierungen festgestellt. Das grundlegende Wesentliche ist, dass ich eine Standardimplementierung einer Protokollmethode bereitgestellt habe, die ich in einer Klasse überschreibe, die das Protokoll implementiert. Diese Protokollerweiterungsmethode wird von einer Basisklasse aufgerufen, die dann eine Methode aufruft, die ich in einer abgeleiteten Klasse überschrieben habe. Das Ergebnis ist, dass die überschriebene Methode nicht aufgerufen wird.

Ich habe versucht, das Problem auf den kleinstmöglichen Spielplatz zu beschränken.

protocol CommonTrait: class {
    func commonBehavior() -> String
}

extension CommonTrait {
    func commonBehavior() -> String {
        return "from protocol extension"
    }
}

class CommonThing {
    func say() -> String {
        return "override this"
    }
}

class ParentClass: CommonThing, CommonTrait {
    override func say() -> String {
        return commonBehavior()
    }
}

class AnotherParentClass: CommonThing, CommonTrait {
    override func say() -> String {
        return commonBehavior()
    }
}

class ChildClass: ParentClass {
    override func say() -> String {
        return super.say()
        // it works if it calls `commonBehavior` here and not call `super.say()`, but I don't want to do that as there are things in the base class I don't want to have to duplicate here.
    }
    func commonBehavior() -> String {
        return "from child class"
    }
}

let child = ChildClass()
child.say() // want to see "from child class" but it's "from protocol extension”

Antworten auf die Frage(6)

Ihre Antwort auf die Frage