swizzling

Swizzling a single instance, not a class

谁说胖子不能爱 提交于 2019-12-03 09:39:03
问题 I have a category on NSObject which supposed to so some stuff. When I call it on an object, I would like to override its dealloc method to do some cleanups. I wanted to do it using method swizzling, but could not figure out how. The only examples I've found are on how to replace the method implementation for the entire class (in my case, it would override dealloc for ALL NSObjects - which I don't want to). I want to override the dealloc method of specific instances of NSObject. @interface

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

App Store - Method Swizzling Legality [closed]

心已入冬 提交于 2019-11-29 09:08:45
Is there any current information on wether or not method swizzling is legal/illegal on the App Store? The only data point I can find is the Three20 framework shakeup a while back, which started with this notice: Your application, xxx, currently posted to the App Store is using method_exchangeImplementations to exchange the implementation of Apple provided APIs with your own implementations. Because of upcoming changes, this behavior in your application may cause a crash or cause user data loss on iPhone OS 4.0. xxx uses method_exchangeImplementations to exchange the implementation of dealloc

My isa-swizzling breaks KVO

这一生的挚爱 提交于 2019-11-29 04:47:00
I'm trying to implement isa swizzling because I need some actions to happen in dealloc method of certain object. I'm overriding - (Class)class; method to return original class (as KVO does). Everything works fine, until I try to add observer to swizzled object. It just crashes. 0x00000000 in 0x00000000 () 0x0091d22a in _NSKeyValueRetainedObservationInfoForObject () 0x0092ec88 in -[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] () 0x0092d6fd in -[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] () Here is implementation of

Is there an alternative to initialize() in macOS now that Swift has deprecated it?

大城市里の小女人 提交于 2019-11-29 01:20:33
Objective-C declares a class function, initialize(), that is run once for each class, before it is used. It is often used as an entry point for exchanging method implementations (swizzling), among other things. Its use was deprecated in Swift 3.1. This is what I used to do: extension NSView { public override class func initialize() { // This is called on class init and before `applicationDidFinishLaunching` } } How can I achieve the same thing without initialize ? I need it for a framework, so requiring calling something in the AppDelegate is a no-go. I need it called before

How do I implement method swizzling?

喜夏-厌秋 提交于 2019-11-28 20:38:30
问题 I am trying to modify behaviour of a program (i dont have it's source) using SIMBL. I used class dump and found out that I need to overide an instance method This method is in the class called controller. All I need to do is get the argument arg1 and thats it. Maybe NSLog it or post a notification... I read about method swizzling in objective-c but how can I use it?. I would need to refer to the class MessageController whose course i don't have. Thanks! 回答1: I'm guessing you need to call the

Is there an alternative to initialize() in macOS now that Swift has deprecated it?

无人久伴 提交于 2019-11-27 21:36:33
问题 Objective-C declares a class function, initialize(), that is run once for each class, before it is used. It is often used as an entry point for exchanging method implementations (swizzling), among other things. Its use was deprecated in Swift 3.1. This is what I used to do: extension NSView { public override class func initialize() { // This is called on class init and before `applicationDidFinishLaunching` } } How can I achieve the same thing without initialize ? I need it for a framework,

Swift function swizzling / runtime

你。 提交于 2019-11-27 19:47:55
Before Swift, in Objective-C I would swizzle or hook methods in a class using <objc/runtime.h> . If anyone has any info on the topic of modifying Swift's runtime and hooking functions like CydiaSubstrate and other libraries that helped in this area, please inform me. I've succeed with method swizzling in Swift. This example shows how to hook description method on NSDictionary My implementation: extension NSDictionary { func myDescription() -> String!{ println("Description hooked") return "Hooooked " + myDescription(); } } Swizzling code: func swizzleEmAll() { var dict:NSDictionary = [