Why should not directly extend UIView or UIViewController?

时间秒杀一切 提交于 2019-11-28 11:40:50

This approach is preferable to using UIView directly, as in

extension UIView {
    func flash() {
        ...
    }
}

because it lets programmers decide which UIView subclasses they wish to make Flashable, as opposed to adding flash functionality "wholesale" to all UIViews:

// This class has flashing functionality
class MyViewWithFlashing : UIView, Flashable {
    ...
}
// This class does not have flashing functionality
class MyView : UIView {
    ...
}

Essentially, this is an "opt in" approach, while the alternative approach forces the functionality without a way to "opt out".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!