swiftprotocolsswift-extensionsprotocol-oriented

Why should not directly extend UIView or UIViewController?


I saw this question, with this code:

protocol Flashable {}

extension Flashable where Self: UIView 
{
    func flash() {
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
            self.alpha = 1.0 //Object fades in
        }) { (animationComplete) in
            if animationComplete == true {
                UIView.animate(withDuration: 0.3, delay: 2.0, options: .curveEaseOut, animations: {
                    self.alpha = 0.0 //Object fades out
                    }, completion: nil)
            }
        }
    }
}

And I wonder why do we you not just directly just extend UIView? Or in similar cases extend UIViewController why twist it around with a where Self:


Solution

  • 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".