ProtocolUI icon indicating copy to clipboard operation
ProtocolUI copied to clipboard

Override method in extension is not a valid approach

Open openaphid opened this issue 9 years ago • 3 comments

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

openaphid avatar Aug 04 '15 04:08 openaphid

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.

VojtaStavik avatar Aug 04 '15 06:08 VojtaStavik

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.

openaphid avatar Aug 05 '15 01:08 openaphid

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.

VojtaStavik avatar Aug 05 '15 07:08 VojtaStavik