ProtocolUI
ProtocolUI copied to clipboard
Override method in extension is not a valid approach
I don't think it's valid to override awakeFromNib from extension UIView : ProtocolUI
, especially UIView
is an Objective-C citizen.
Please refer to other discussions like: https://groups.google.com/forum/#!topic/swift-language/mh7Sxnhli3M
I was thinking about this a lot. In fact, this is not an overriding, because there isn't a default implementation of awakeFromNib()
in UIKit. So using it in the extension doesn't affect the default behaviour. You can still override it in your custom views and as far as you call super.awakeFromNib()
, you can use the ProtocolUI approach for styling your view.
Swift compiler is OK with this, too.
UIKit is an Obj-C framework, any internal implementation can be hidden from pubic headers.
Although the existence of -[UIView awakeFromNib]
can be confirmed via Obj-C runtime API, it's not future proof.
I've seen some libraries overrode -[UIScrollView touchesBegan:withEvents:] with extensions for old iOS versions, it worked for a while and compiler was OK with it. Then it breaks system handwriting keyboard on iOS 8.
I agree that this solution isn't the safest one possible. Unfortunately, I couldn't find any other way how to make this work. I'm open to any suggestions or pull requests.