If I override a class method, is there a way I can call the original method (the one that was overridden)?
In Objective-C, if I override a class method using a category, is there a way I can call the original method (the one that was overridden)?
- Value of type 'GTLDriveFile' has no member 'downloadUrl'
- iOS CSS opacity + visibility transition
- How to update our static library architecture for suporting arm64 Build?
- I get nil when using NSDateFormatter in swift
- ContainerView embedded in ViewController: Outlets are nil
- Top space on device rotation
3 Solutions Collect From Internet About “If I override a class method, is there a way I can call the original method (the one that was overridden)?”
I present you with three icky ways to do this in
+(void)load. In every case, name your method MyCategory_method or so.
- class_getMethodImplementation() and class_replaceMethod(). Store the old IMP, and call it directly. You need to get the method’s type encoding. Note that you can just use a normal C function too…
- class_getInstanceMethod(), method_getImplementation(), method_setImplementation(). As above, but you don’t need to get the method’s type encoding.
- class_getInstanceMethod() on both methods, and then method_exchangeImplementations(). Call MyCategory_method to get the original implementation. This is the easiest way to do it.
Sometimes, it’s the only reasonably easy way to make it do what you want…
EDIT: And only do this if you know what you’re doing!
Doesn’t look like it is possible.
When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that already existed in the category’s class, there is no way to invoke the original implementation.
What this is saying to me is that if you override a method on a subclass via a category, you can call [super methodName] as you would normally, but if you override the base class method directly, you can’t invoke the original.
If you dynamically provide the category override (see resolveInstanceMethod:), you can cache the previous method selector beforehand, and call that.
- svn:ignore not ignoring xcuserdata
- UIRefreshControl for “Pull Up” to refresh?
- Convert Character to Int in Swift
- Grand Central Dispatch vs. NSThread
- Xcode 6.4 – Autocomplete Doesn't Show Type Symbols
- how to set border style of a UITextfield
- How do I implement Swift's Comparable protocol?
- Xcode Unit Tests: Xcode doesn't stop at breakpoints
- Weird colors in XCode Interface Builder?
- How to convert NSString HTML markup to plain text NSString?
- Convert the “lite app” to “pro app” with in the application
- How to set iPhone UI View z index?
- OTHER_CODE_SIGN_FLAGS keychain flag ignored?
- How debug after closing application
- Memory leak on presentModalViewController