I have some class (class A: UIView) that conforms protocol UIGestureRecognizerDelegate from third-party library. I need to extend this class by adding some optional UIGestureRecognizerDelegate method, for example gestureRecognizer:shouldRequireFailureOfGestureRecognizer:. How to do that?
I have tried writing an extension for A:
extension A {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
print("abcd") // BREAKPOINT NOT REACHABLE
}
}
Then an extension for a protocol (even without Self == A):
extension UIGestureRecognizer where Self == A {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
print("abcd") // BREAKPOINT NOT REACHABLE
}
}
The only thing worked is subclassing A:
class B: A {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
print("abcd") // Hallelujah! BREAKPOINT IS reachable
}
}
But I have multiple views that have to be inherited from A. And this leads to copy and paste one logic for each inherited class. I don't want to repeat myself :)
After all efforts I found a satisfactory solution. So, in third-party library I had
class A: UIGestureRecognizerDelegate { ... }
And either class B: A and class C: A in that library.
So I hane wrote
extension A {
func myHelperFunction(with some: Parameters) -> Bool {
...
}
}
extension B {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
myHelperFunction(with some: Parameters)
}
}
extension C {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
myHelperFunction(with some: Parameters)
}
}
That's all. Minimum of code and self repeating.