在NSInvocation中为具有可变参数的方法传递多个参数

Iro*_*Man 3 macos objective-c ios

我有一个方法,它采用类似这样的变量参数,参数以nil结尾.

-(void)manyParams:(NSString *)st, ... {
    va_list argList;
    va_start(argList,st);

    id obj;

    while ((obj = va_arg(argList, id))) {
        NSLog(@"%@",obj);
    }
    va_end(argList);

    return;
}
Run Code Online (Sandbox Code Playgroud)

我可以像这样直接调用它

[self manyParams:@"one",@"two",@"three",nil];
Run Code Online (Sandbox Code Playgroud)

如果我使用NSInvocation类来调用manyParams,那么我该怎么做呢

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:@selector(manyParams:)];
///NSString *one = @"one";
///[invocation setArgument:&one atIndex:2]; //////How to pass variable arguments like @"one",@"two",@"three", nil
[invocation invoke];
Run Code Online (Sandbox Code Playgroud)

ipm*_*mcc 5

NSInvocation不支持可变方法,因此这是不可能的.(参考:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html)

NSInvocation不支持使用可变数量的参数或联合参数调用方法.

如果有一个方法的替代版本需要a va_list并且你的所有参数都是对象指针你可能会像我在这里的答案那样伪造一些东西:假的va_list在ARC中