forward invocation, by hand vs magically?
- by John Smith
I have the following two class:
//file FruitTree.h
@interface FruitTree : NSObject
{
Fruit * f;
Leaf * l;
}
@end
//file FruitTree.m
@implementation FruitTree
//here I get the number of seeds from the object f
@end
//file Fruit
@interface Fruit : NSObject
{
int seeds;
}
-(int) countfruitseeds;
@end
My question is at the point of how I request the number of seeds from f. I have two choices.
Either: Since I know f I can explicitly call it, i.e. I implement the method
-(int) countfruitseeds
{
return [f countfruitseeds];
}
Or: I can just use forwardInvocation:
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
// does the delegate respond to this selector?
if ([f respondsToSelector:selector])
return [f methodSignatureForSelector:selector];
else if ([l respondsToSelector:selector])
return [l methodSignatureForSelector:selector];
else
return [super methodSignatureForSelector: selector];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
[invocation invokeWithTarget:f];
}
(Note this is only a toy example to ask my question. My real classes have lots of methods, which is why I am asking.)
Which is the better/faster method?