我正在使用傳統的Swift 2.2項目,並且我想爲我的代碼實現一些衆所周知的面向協議的實踐。UIViewController的自我約束協議擴展
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!")
}
}
雖然,它是在雨燕3.1的作品,在這裏我得到一個錯誤:The NewViewController doesn't conform to protocol SuccessPresenting
但我爲什麼要編寫協議實現在我的VC,因爲我已經做了,使用協議擴展? 我會感謝任何幫助。 請注意,這是Swift 2.2
嘗試從您的NewViewController中刪除'SuccessPresenting'一致性約束。即'類NewViewController:UIViewController {//調用presentSuccess的代碼}' –
@NandiinBao這並沒有幫助我 –