我正在阅读Ray Wenderlich教程,并且注意到作者使用类扩展来保存委托回调,而不是让它们在类本身中进行处理,即:
类扩展内的委托回调:
extension LogsViewController : UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        ...
    }
}而不是将其包含在类中:
在类内委托回调:
class LogsViewController : UITableViewController, UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        ...
    }
}我同时发现这很奇怪也很有趣。他有一个专门用于LogsViewController类的扩展的文件,名为“ LogsViewControllerExtension.swift”,并且每个委托协议都有一个不同的扩展名:UITableViewDataSource,UISplitViewDelegate等,即:
多个类扩展,每个都有自己的文件中的委托回调:
extension LogsViewController: UISplitViewControllerDelegate {
    ... callbacks
}
extension LogsViewController : UIPopoverPresentationControllerDelegate {
    ... callbacks
}为什么?
这样做有什么好处?我可以看到将其分开的可读性更高,但同时又是间接的。是否有OO原则支持或反对这样做?