我正在查看这个链接。
但是我不太理解以下两个代码片段之间的逻辑差异:
1. 仅扩展符合 ErrorPopoverRenderer 协议的那些 UIViewControllers。
protocol ErrorPopoverRenderer {
func presentError(message: String, withArrow shouldShowArrow: Bool, backgroundColor: UIColor, withSize size: CGSize, canDismissByTappingAnywhere canDismiss: Bool)
}
extension UIViewController: ErrorPopoverRenderer { //Make all the UIViewControllers that conform to ErrorPopoverRenderer have a default implementation of presentError
func presentError(message: String, withArrow shouldShowArrow: Bool, backgroundColor: UIColor, withSize size: CGSize, canDismissByTappingAnywhere canDismiss: Bool)
{}
}
2. 仅扩展符合协议的UIViewController的协议。
extension ErrorPopoverRenderer where Self: UIViewController {
func presentError() {
}
}
无论哪种方式,任何遵守该协议的UIViewController子类都将具有默认的方法实现,但是在UIviewcontroller扩展或协议扩展中。 逻辑上有什么区别? 如果我有错误,请纠正我。