double forwarding problem
double forwarding problem
- Subject: double forwarding problem
- From: Ivan Kourtev <email@hidden>
- Date: Fri, 3 Nov 2006 19:07:19 -0500
I have objects fooA, fooB, and fooC, of classes A, B, and C,
respectively. I want some messages sent to fooA to be forwarded to
fooB, and some messages sent to fooB to be forwarded to fooC. This
requires me to implement methodSignatureForSelector and
forwardInvocation in both A and B. I implement these methods as
follows (the implementation for class B differs in that it checks
with fooC, not with fooB):
@implementation A
...
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [fooB methodSignatureForSelector:selector];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL selector = [invocation selector];
if ([fooB respondsToSelector:selector]) {
[invocation invokeWithTarget:fooB];
} else {
[self doesNotRecognizeSelector:selector];
}
}
...
@end
So I have a method 'x' that only fooB implements and can respond to
(fooA and fooC don't implement it). I send [fooA x] and expect to
see - [B x] executed. Instead, here is what happens:
1. methodSignatureForSelector is sent to fooA
2. fooA sends methodSignatureForSelector to fooB
3. fooB sends methodSignatureForSelector to fooC
4. because fooC doesn't implement 'x', nil is returned all the way up
and - [B x] is never reached.
The problem is that methodSignatureForSelector is sent before
forwardInvocation so I don't have a chance to simply change the
invocation target. At this point, the only thing that comes to mind
is to change the implementation of - [B methodSignatureForSelector]
to check whether B responds to the selector (as in the example at the
end). But then what do I return if I find out that
respondsToSelector is YES? I can't return [self
methodSignatureForselector:selector] because it will start calling
itself recursively.
I can also try to check whether fooB respondsToSelector in fooA's
methodSignatureForSelector and then [fooB
performWithSelector:selector] but I don't have access to the arguments.
Is this kind of double forwarding a bad idea in principle or am I
missing some [hopefully more elegant] way to resolve this?
-- ivan
@implementation B
...
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
if ([self respondsToSelector:selector]) {
return ???; // <= WHAT TO RETURN HERE?
} else {
return [fooC methodSignatureForSelector:selector];
}
}
...
@end
_______________________________________________
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