method-swizzling

Method swizzling in swift 4 [duplicate]

折月煮酒 提交于 2019-12-03 07:52:06
This question already has answers here : Swift 3.1 deprecates initialize(). How can I achieve the same thing? (9 answers) Swizzling in Swift 4 no longer works. Method 'initialize()' defines Objective-C class method 'initialize', which is not permitted by Swift This is something I have found a solution to so wanted to leave the questions and answer for others. initialize() is no longer exposed: Method 'initialize()' defines Objective-C class method 'initialize', which is not permitted by Swift So the way to do it now is to run your swizzle code via a public static method. e.g In the extension:

How to swizzle NSURLSession class method dataTaskWithUrl

无人久伴 提交于 2019-12-02 17:37:11
问题 I have been trying to swizzle NSURLSession class method dataTaskWithRequest but not been able to get it done extension NSURLSession{ public override class func initialize() { struct Static { static var token: dispatch_once_t = 0 } if self !== NSURLSession.self { return } dispatch_once(&Static.token) { let originalSelector = Selector("dataTaskWithRequest:completionHandler:") let swizzledSelector = Selector("my_dataTaskWithRequest:completionHandler:") let originalMethod = class

How to call original implementation when overwriting a method with a category?

故事扮演 提交于 2019-11-30 09:59:33
I try to figure out how things really work. So I thought when I would overwrite certain methods using categories, I would get interesting NSLogs. @implementation UIView(Learning) - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { NSLog(@"-hitTest:withEvent: event=%@", event); return [self hitTest:point withEvent:event]; } @end super and self don't work here. Is there a way to call the original implementation of -hitTest:withEvent:? What I want is an NSLog every time -hitTest:withEvent: is called on an UIView. It's just for personal learning purposes. I want to see the event

Method swizzling in Swift

谁说我不能喝 提交于 2019-11-28 20:37:53
问题 There is a little problem with UINavigationBar which I'm trying to overcome. When you hide the status bar using prefersStatusBarHidden() method in a view controller (I don't want to disable the status bar for the entire app), the navigation bar loses 20pt of its height that belongs to the status bar. Basically the navigation bar shrinks. I was trying out different workarounds but I found that each one of them had drawbacks. Then I came across this category which using method swizzling, adds a