swizzling

My isa-swizzling breaks KVO

坚强是说给别人听的谎言 提交于 2019-11-27 18:56:08
问题 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

How to swizzle a class method on iOS?

自作多情 提交于 2019-11-27 11:14:34
Method swizzling works great for instance methods. Now, I need to swizzle a class method. Any idea how to do it? Tried this but it doesn't work: void SwizzleClassMethod(Class c, SEL orig, SEL new) { Method origMethod = class_getClassMethod(c, orig); Method newMethod = class_getClassMethod(c, new); if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); else method_exchangeImplementations(origMethod, newMethod); } Turns out, I wasn't far away. This

How to implement method swizzling swift 3.0?

前提是你 提交于 2019-11-26 21:28:42
How can I implement method swizzling in Swift 3.0 ? I've read nshipster article about it, but in this code's chunk struct Static { static var token: dispatch_once_t = 0 } the compiler gives me an error dispatch_once_t is unavailable in Swift: Use lazily initialized globals instead Tikhonov Alexander First of all dispatch_once_t is unavailable in Swift 3.0. You can choose from two alternatives: Global variable Static property of struct , enum or class For more details, see that Whither dispatch_once in Swift 3 For different purposes you must use different implementation of swizzling Swizzling

How to swizzle a class method on iOS?

那年仲夏 提交于 2019-11-26 17:59:38
问题 Method swizzling works great for instance methods. Now, I need to swizzle a class method. Any idea how to do it? Tried this but it doesn't work: void SwizzleClassMethod(Class c, SEL orig, SEL new) { Method origMethod = class_getClassMethod(c, orig); Method newMethod = class_getClassMethod(c, new); if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding

Method Swizzle on iPhone device

為{幸葍}努か 提交于 2019-11-26 17:04:40
I tried both JRSwizzle, and MethodSwizzle. They compile fine on the simulator but throw a bunch of errors when I try to compile for Device (3.x) Has anyone had any luck swizzling on the iphone? Whats the trick? TIA The CocoaDev wiki has an extensive discussion on method swizzling here . Mike Ash has a relatively simple implementation at the bottom of that page: #import <objc/runtime.h> #import <objc/message.h> //.... void Swizzle(Class c, SEL orig, SEL new) { Method origMethod = class_getInstanceMethod(c, orig); Method newMethod = class_getInstanceMethod(c, new); if(class_addMethod(c, orig,

How to implement method swizzling swift 3.0?

混江龙づ霸主 提交于 2019-11-26 07:56:20
问题 How can I implement method swizzling in Swift 3.0 ? I\'ve read nshipster article about it, but in this code\'s chunk struct Static { static var token: dispatch_once_t = 0 } the compiler gives me an error dispatch_once_t is unavailable in Swift: Use lazily initialized globals instead 回答1: First of all dispatch_once_t is unavailable in Swift 3.0. You can choose from two alternatives: Global variable Static property of struct , enum or class For more details, see that Whither dispatch_once in

Method Swizzle on iPhone device

谁说胖子不能爱 提交于 2019-11-26 05:16:05
问题 I tried both JRSwizzle, and MethodSwizzle. They compile fine on the simulator but throw a bunch of errors when I try to compile for Device (3.x) Has anyone had any luck swizzling on the iphone? Whats the trick? TIA 回答1: The CocoaDev wiki has an extensive discussion on method swizzling here. Mike Ash has a relatively simple implementation at the bottom of that page: #import <objc/runtime.h> #import <objc/message.h> //.... void Swizzle(Class c, SEL orig, SEL new) { Method origMethod = class

What are the Dangers of Method Swizzling in Objective-C?

社会主义新天地 提交于 2019-11-26 02:59:22
问题 I have heard people state that method swizzling is a dangerous practice. Even the name swizzling suggests that it is a bit of a cheat. Method Swizzling is modifying the mapping so that calling selector A will actually invoke implementation B. One use of this is to extend behavior of closed source classes. Can we formalise the risks so that anyone who is deciding whether to use swizzling can make an informed decision whether it is worth it for what they are trying to do. E.g. Naming Collisions