是否可以使用@selector
and performSelector:
(或类似)使用变量参数列表的方法?
我正在编写一个可以分配委托来覆盖默认行为的类。在存在委托选择方法的情况下,对该类的实例进行的调用将被转发到相同的相应委托方法,其中一些方法使用可变参数列表。
因此,例如,我需要能够创建检索SEL
使用如下方法引用委托对象并向其发送消息:
- (void)logEventWithFormat:(NSString *)format, ... {
va_list argList;
id del = self.delegate;
if (del != nil &&
[del conformsToProtocol:@protocol(AProtocolWithOptionalMethods)] &&
[del respondsToSelector:@selector(logEventWithFormat:)])
{
// Perform selector on object 'del' with 'argList'
}
}
我假设这是不可能的,因此基础框架中的类似方法声明 - 在NSString
:
- (id)initWithFormat:(NSString*)format, ...;
and
- (id)initWithFormat:(NSString *)format arguments:(va_list)argList;
我认为我希望委托的协议应该建议实施:
- (void)logEventWithFormat:(NSString *)format arguments:(va_list)argList;
所以我是选择器@selector(logEventWithFormat:arguments:)
可以使用以下方式调用:
[del performSelector:@selector(logEventWithFormat:arguments:)
withObject:format
withObject:argList];
我只是想知道我是否错过了一些东西,或者为了实现我想要的目标而走了很长的路?