UIViewController的自我约束协议扩展
问题描述:
我正在使用传统的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
答
这是直接粘贴吗?因为你的extension
包含一个可选的而不是一个常规的字符串,而你的协议有一个正常的String
。这可能会导致编译器认为它是一种不同的方法,在这种情况下使协议的optionallness
无效。
+0
哦,谢谢上帝stackOverFlow存在。当然,你是对的,我是不留神) –
尝试从您的NewViewController中删除'SuccessPresenting'一致性约束。即'类NewViewController:UIViewController {//调用presentSuccess的代码}' –
@NandiinBao这并没有帮助我 –