假设我有一个带有此签名的方法:
-(void)plotPoly:(Polygon *)poly WithColor:(UIColor *)color AndFill:(BOOL)filled;
如何在其中获取 UIColor 和 BOOL 以及 Polygon?
我应该将它们包装在 NSArray 中,然后将它们拉出到调用的方法中吗?那意味着我必须更改方法 sig,对吗?
有没有更优雅的方法呢?
最佳答案
仍然不完全是我所说的优雅,但比必须更改整个 API 更讨厌的是 NSInvocation:
Polygon *poly;
UIColor *color;
BOOL filled;
// Assume the above variables exist
NSInvocation *inv = [NSInvocation invocationWithMessageSignature:[target messageSignatureForSelector:message]];
[inv setArgument:&poly atIndex:2];
[inv setArgument:&color atIndex:3];
[inv setArgument:&filled atIndex:4];
[inv performSelector:@selector(invokeWithTarget:) withObject:target afterDelay:1];
另一个最佳选择是创建一个包装器方法,它使用适当的参数(可能以字典或数组的形式给出)调用您想要的原始方法,这与延迟后执行所需的签名相匹配。
关于objective-c - 如何使用performSelector :withObject:afterDelay: on a method with multiple arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1584455/