我不确定这是否可能,但在Ruby中,你可以使用send动态调用方法。
例如,如果我想要为对象foo调用bar方法,我可以使用:
foo.send("bar")
有没有使用Objective-C实现类似功能的方法?
谢谢!
我不确定这是否可能,但在Ruby中,你可以使用send动态调用方法。
例如,如果我想要为对象foo调用bar方法,我可以使用:
foo.send("bar")
performSelector:
方法。然而,这种方法只适用于没有或只有少数参数的方法。objc_msgSend()
,但直接调用它可能不是一个好主意,因为运行时可能在幕后做了其他处理。对于一般用途(具有返回值和任意数量参数的方法),请使用NSInvocation:
if ([target respondsToSelector:theSelector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
[target methodSignatureForSelector:theSelector]];
[invocation setTarget:target];
[invocation setSelector:theSelector];
// Note: Indexes 0 and 1 correspond to the implicit arguments self and _cmd,
// which are set using setTarget and setSelector.
[invocation setArgument:arg1 atIndex:2];
[invocation setArgument:arg2 atIndex:3];
[invocation setArgument:arg3 atIndex:4];
// ...and so on
[invocation invoke];
[invocation getReturnValue:&retVal]; // Create a local variable to contain the return value.
}
if ([foo respondsToSelector:@selector(bar)])
[foo performSelector:@selector(bar))];
performSelector:
、performSelector:withObject:
和performSelector:withObject:withObject:
这几个方法可以使用——超过两个参数就不再是可行的选择了。我记得 NextSTEP 曾经有一个performv:
方法,允许使用可变参数之类的东西,但我不太确定…… - ephemient