method-swizzling

Swizzling UIImage init not working iOS Swift

六月ゝ 毕业季﹏ 提交于 2020-02-24 18:38:30
问题 I am trying to swizzle UIImage.init(named:) but the init is not being called extension UIImage { @objc public convenience init?(swizzledName: String) { self.init(named: swizzledName) /// Do something print("this is working") } static func swizzle() { guard let instance = class_getClassMethod(self, #selector(UIImage.init(named:))), let swizzledInstance = class_getClassMethod(self, #selector(UIImage.init(swizzledName:))) else { return } method_exchangeImplementations(instance, swizzledInstance)

Swizzling UIImage init not working iOS Swift

会有一股神秘感。 提交于 2020-02-24 18:36:24
问题 I am trying to swizzle UIImage.init(named:) but the init is not being called extension UIImage { @objc public convenience init?(swizzledName: String) { self.init(named: swizzledName) /// Do something print("this is working") } static func swizzle() { guard let instance = class_getClassMethod(self, #selector(UIImage.init(named:))), let swizzledInstance = class_getClassMethod(self, #selector(UIImage.init(swizzledName:))) else { return } method_exchangeImplementations(instance, swizzledInstance)

Proper way of method swizzling in objective-C

橙三吉。 提交于 2019-12-28 11:59:25
问题 Currently experimenting with method swizzling in Objective-C and I have a question. I am trying to understand the proper way to method swizzle and after researching online I stumbled upon this NSHipster post: http://nshipster.com/method-swizzling/ In the post the author has some method swizzling sample code. I am looking for someone to better explain to me what the author is doing.. In particular I am confused on the didAddMethod logic. Why is the author not just directly swapping/exchanging

Proper way of method swizzling in objective-C

微笑、不失礼 提交于 2019-12-28 11:59:09
问题 Currently experimenting with method swizzling in Objective-C and I have a question. I am trying to understand the proper way to method swizzle and after researching online I stumbled upon this NSHipster post: http://nshipster.com/method-swizzling/ In the post the author has some method swizzling sample code. I am looking for someone to better explain to me what the author is doing.. In particular I am confused on the didAddMethod logic. Why is the author not just directly swapping/exchanging

Is there any way to list all swizzled methods in an iOS app?

荒凉一梦 提交于 2019-12-18 15:46:24
问题 I'm essentially looking for a way to detect when/what third party libraries swizzle. I recently ran into a situation where an ad library used an oddball fork of AFNetworking. AFNetworking swizzles NSURLSessionTask, and the two swizzles didn't play nicely under certain circumstances. I'd really like to be able to detect and sanity check this kind of thing, and ideally even keep a versioned dump of every swizzled method in the app so we have some visibility into who's monkey patching what and

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

扶醉桌前 提交于 2019-12-18 13:34:30
问题 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

Method swizzling in swift 4 [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 06:07:49
问题 This question already has answers here : Swift 3.1 deprecates initialize(). How can I achieve the same thing? (9 answers) Closed 2 years ago . 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. 回答1: initialize() is no longer exposed: Method 'initialize()' defines Objective-C class method 'initialize', which

Swizzled method for NSMutableDictionary is not getting called

怎甘沉沦 提交于 2019-12-08 09:56:39
问题 I'm trying to swizzle NSMutableDictionary. What am I doing wrong here? I'm trying to override setObject:forKey: for NSMutableDictionary. #import "NSMutableDictionary+NilHandled.h" #import <objc/runtime.h> @implementation NSMutableDictionary (NilHandled) + (void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; SEL originalSelector = @selector(setObject:forKey:); SEL swizzledSelector = @selector(swizzledSetObject:forKey:); Method originalMethod =

Method Swizzling does not work

谁都会走 提交于 2019-12-07 01:58:34
问题 I would like to make use of method swizzling, but am unable to get even simple examples to work for me. It is possible that I am misunderstanding what the concept is, but as far as I know it allows for method implementations to be swapped. Given two methods, A and B, I would like to swap their implementations such that calling A would execute B instead. I came across a few examples of swizzling (example1 and example2). I created a new project with a class to test this. class Swizzle: NSObject