UIViewController 的自我约束协议扩展

self constraint protocol extention for UIViewController

我正在处理遗留 Swift 2.2 项目,我想在我的代码中实现一些众所周知的面向协议的实践。

protocol SuccessPresenting {
    func presentSucess(title: String, message: String)
}

extension SuccessPresenting where Self: UIViewController {
    func presentSucess(title: String?, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let dismissAction = UIAlertAction(title: "ОК", style: .Default, handler: nil)
        alertController.addAction(dismissAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

class NewViewController: UIViewController, SuccessPresenting {

   func foo() {
      presentSucess(nil, message: "Done!")
   }
}

尽管它适用于 Swift 3.1,但在这里我得到一个错误:The NewViewController doesn't conform to protocol SuccessPresenting

但是为什么我要在我的 VC 中编写协议实现,因为我已经使用协议扩展完成了? 我将不胜感激任何帮助。 请提醒,这是Swift 2.2

这是直接粘贴吗?因为你的 extension 包含一个可选字符串而不是常规字符串,而你的协议有一个正常的 String。这可能会导致编译器认为它是一种不同的方法,从而使您的协议的 optionallness 在这种特殊情况下无效。