I love building reusable components based on protocols, that can provide additional functionality for adopters. And sometimes I want to add for example gestures on UIView subclasses adopters.
So I have to add method that adds gesture recognizer and sets it target and action to handle it. Remember that action property is Selector and the method we referring in the selector must be available to Objective-C, thus marked with @objc modifier. But when we add the modifier we have the following error:
So for now it seems that there is no way to solve the puzzle. But actually there is the solution I found.
What we are to do is to create Objective-C class that will act like a proxy, calling closures we provided on initialization when particular method is called. So that is the class:
Now we have to integrate it in our extension somehow. We all know that we cannot have stored properties there, but we also all know that we can use ObjC API to do it.
So there is the complete extension.
Notice how we set target and action on line 49. Also notice how we initially set proxy object in its getter as we don’t have any other entry point to do this.
So now all I have to do in my views to make then be refreshable by swipe is to call recognizer adding and implement my refresh logic.
This is simple example of how you can solve the problem stated above.
Hope I help someone! Thank you for reading.