2
我有以下两类:转发调用,手动vs神奇?
//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
我的问题是我如何申请根据f种子数点。我有两个选择。
或者:因为我知道˚F我可以明确地调用它,也就是我实现方法
-(int) countfruitseeds
{
return [f countfruitseeds];
}
或者:我可以只使用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];
}
(注意:这仅仅是一个玩具的例子来问我的问题,我真正的课程有很多的方法,这就是为什么我问。)
哪种方法更好/更快?
你有没有很好的参考? – 2010-05-28 03:01:31
什么是直接引用?发送单个消息比将包裹调用放入NSInvocation对象并发送更多消息更快?这里有一个很好的参考:http://www.mikeash.com/pyblog/performance-comparisons-of-common-operations.html(NSInvocation出来比正常的消息发送慢160倍,这不包括创建调用AFAICT) – Chuck 2010-05-28 08:08:20
感谢您的链接@Chuck。应该注意的是,它调用的时间大约是160ns,而不是慢160倍。实际上,它比一个客观的c信息慢30倍......这仍然非常可怕。 – tyler 2015-08-24 20:06:01