I'd like to override a method in an Objective C class that I don't have the source to.
I've looked into it, and it appears that Categories should allow me to do this, but I'd like to use the result of the old method in my new method, using super to get the old methods result.
Whenever I try this though, my method gets called, but "super" is nil... Any idea why? I'm doing iPhone development with the XCode 2.2 SDK. I'm definitely working with an instance of a class, and the method of the class is an instance method.
@implementation SampleClass (filePathResolver) -(NSString*) fullPathFromRelativePath:(NSString*) relPath { NSString *result = [super fullPathFromRelativePath: relPath]; ... do some stuff with the old result return result; }
Note and clarification: From what I can see in the Apple Docs, it appears to me that this should be allowed?
Categories docs at developer.apple.com: 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.
In '[super viewDidLoad]', 'super' means the same object as 'self', so the class of this 'super' is B. However, by using 'super', you're telling the compiler that you do not want to invoke class B's 'viewDidLoad' method, but its superclass's 'viewDidLoad'. That means UIView's viewDidLoad, not A's.
A category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing. Check the apple doc for the Category in Objective-C.
Categories extend the original class, but they don't subclass it, therefore a call to super
doesn't find the method.
What you want is called Method Swizzling. But be aware that your code could break something. There's an article on Theocacao written by Scot Stevenson about Method Swizzling in the old Objective-C runtime, Cocoa with Love by Matt Gallagher has an article about Method Swizzling in the new Objective-C 2.0 runtime and a simple replacement for it.
Alternatively, you could subclass the class and then either use the subclass or use + (void)poseAsClass:(Class)aClass
to replace the superclass. Apple writes:
A method defined by a posing class can, through a message to
super
, incorporate the superclass method it overrides.
Be aware that Apple has deprecated poseAsClass:
in Mac OS X 10.5.
If you will be coding against this class, simply rename the selector to something your code can use, and call the original selector on self
:
@implementation SampleClass (filePathResolver) -(NSString*) myFullPathFromRelativePath:(NSString*) relPath { NSString *result = [self fullPathFromRelativePath: relPath]; ... do some stuff with the old result return result; }
If you want to override the default implementation of this selector for that class, you'll need to use the method swizzling approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With