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:
- Is it so that we increase our intent and when other developers come they would see that hey this class is conforming to Flashable, Dimmable, etc?
- Also our UIView would have separate meaningful extensions? Instead of different unNamed extensions to UIView or UIViewController?
- Are there any specific Apple guidelines on this subject for POP? I've seen there developer doing it this way but not sure of the why...
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 UIView
s:
// 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".
来源:https://stackoverflow.com/questions/41706504/why-should-not-directly-extend-uiview-or-uiviewcontroller