forward invocation, by hand vs magically?
Posted
by John Smith
on Stack Overflow
See other posts from Stack Overflow
or by John Smith
Published on 2010-05-28T01:41:14Z
Indexed on
2010/05/28
1:51 UTC
Read the original article
Hit count: 284
objective-c
|cocoa
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?
© Stack Overflow or respective owner