Swift 覆盖协议扩展保持扩展行为

Swift overriding protocol extension keeping the extension behaviour

我有这个简单的 class 和一个名为 'ShowAlert' 的假设协议,它是执行默认实现和默认 ViewController 的扩展,它是 ShowAlert 协议实现。

protocol ShowAlert {
    var titleForAlert: String! { get }
    func messageForAlert() -> String!
    func show()
}

extension ShowAlert where Self: UIViewController {

    func show(){
        let alert = UIAlertController(title: self.titleForAlert, message: self.messageForAlert(), preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .Cancel, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }


    @IBAction func showItNow(sender: AnyObject) {
        self.show()
    }



}

extension ViewController: ShowAlert {
    var titleForAlert: String! {
        get{
            return "Foo"
        }
    }


    func messageForAlert() -> String! {
        return "Bar"
    }

    func show() {
    // here I want to call the default implementation of the protocol to show the alert, then do something else
        print("Good day sir!")
    }
}

这就像在子class 上,我可以调用 'super.show()' 然后继续执行我想做的任何事情。

有什么办法吗?还是我的逻辑违背了协议的设计目的,而这不应该发生?

有一个简单的解决方案:只需在扩展中添加一个 defaultShow 方法即可。

extension ShowAlert where Self: UIViewController {

    func defaultShow(){
        let alert = UIAlertController(title: self.titleForAlert, message: self.messageForAlert(), preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .Cancel, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }

    func show() {
        defaultShow()
    }
}

所以在你的代码中你可以调用 defaultShow:

extension ViewController: ShowAlert {
    // ...

    func show() {
        self.defaultShow()
        print("Good day sir!")
    }
}

还有另一种解决方案,您可以调用 .show() 而不是 .defaultShow()。但是它使用铸造并破坏封装。如果你想看,请告诉我。