Re: Calling original method implementation from category
Re: Calling original method implementation from category
- Subject: Re: Calling original method implementation from category
- From: Axel Andersson <email@hidden>
- Date: Mon, 25 Apr 2005 14:12:10 +0200
On Apr 25, 2005, at 10:31, Georg Tuparev wrote:
According to Anguish at all, this is supposed to work. But in reality
(*_originalDomainMethod)(self, _cmd) enters in an endless recursion.
I think the problem here stems from you overriding -domain in a
category, and then, later, assuming class_getInstanceMethod() will
return the original -domain. It will, in fact, return your overridden
method. You need to name your selector something else in order to be
able to hold on to the old method. Here's some code I use for this,
with your NSError category at the end:
#import <objc/objc-runtime.h>
IMP ZAReplaceMethodImplementationInClass(Class class, SEL selector, IMP
newImp) {
struct objc_method *method;
IMP imp;
method = class_getInstanceMethod(class, selector);
if(!method) {
NSLog(@"*** ZAReplaceMethodImplementationInClass: Could not find
method implementation for %@ in class %@",
NSStringFromSelector(selector), NSStringFromClass(class));
return NULL;
}
imp = method->method_imp;
method->method_imp = newImp;
return imp;
}
IMP ZAReplaceSelectorInClass(Class class, SEL selector, SEL
newSelector) {
struct objc_method *method;
method = class_getInstanceMethod(class, newSelector);
if(!method) {
NSLog(@"*** ZAReplaceSelectorInClass: Could not find method
implementation for %@ in class %@",
NSStringFromSelector(newSelector), NSStringFromClass(class));
return NULL;
}
return ZAReplaceMethodImplementationInClass(class, selector,
method->method_imp);
}
@implementation NSError(SELogEntry)
static IMP _originalDomainMethod;
+ (void)load {
NSLog(@"===> Loading NSError category");
_originalDomainMethod = ZAReplaceSelectorInClass([self class],
@selector(domain), @selector(SE_domain));
}
- (NSString *)SE_domain {
if(_originalDomainMethod)
return (*_originalDomainMethod)(self, _cmd);
return nil;
}
@end
Hope this helps,
--
Axel Andersson
email@hidden
http://www.zankasoftware.com/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden