I am developing an Cocoa Touch Framework or a dynamic Library which would capture user actions using Swift.
E.g. When a user taps on a label, my framework should know about it. For achieving this behaviour, I am using method swizzling.
I am creating an extension for the UITapGestureRecognizer
.In this extension, in initialize
method , I am swizzling the init :
open override class func initialize() {
guard self === UITapGestureRecognizer.self else { return }
let originalSelector = #selector(self.init(target:action:))
let swizzledSelector = #selector(swizzled_init(target:action:))
swizzling(self, originalSelector, swizzledSelector)
}
func swizzled_init(target: Any, action: Selector) {
swizzled_init(target : target,action: action)
print("swizzled_init"+action.description)
//This part is for swizzling the the action which is being provided by the developer at the time of init.
guard self === UITapGestureRecognizer.self else { return }
let originalSelector = action
let swizzledSelector = #selector(swizzled_action(sender:))
swizzling(self.classForCoder, originalSelector, swizzledSelector)
}
func swizzled_action(sender : UITapGestureRecognizer? = nil){
print("swizzled_action"+(sender?.description)!)
}
After swizzling init, the instance of tap is nil in my ViewController.
let tap = UITapGestureRecognizer.init(target: self, action: #selector(buttonTapped(sender:)))
And the second issue is this in the swizzled_init method:
guard self === UITapGestureRecognizer.self else { return }
which is getting failed.
Could you please direct me in resolving these issues?