为具有变量参数的方法在nsincall中传递多个参数

时间:2022-04-12 23:15:43

I have a method which take variable arguments something like this, the arguments end with nil.

我有一个方法,它采用变量参数,像这样,参数以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;
}

I can call it directly like this

我可以直接这样称呼它

[self manyParams:@"one",@"two",@"three",nil];

If I am using NSInvocation class to invoke the manyParams then how can I do this

如果我正在使用nsinvocclass来调用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];

1 个解决方案

#1


5  

NSInvocation does not support variadic methods, so this will not be possible. (Reference: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html )

NSInvocation不支持可变的方法,所以这是不可能的。(参考:https://developer.apple.com/library/mac/文档/可可/引用/基础/类/ NSInvocation_Class /引用/ Reference.html)

NSInvocation does not support invocations of methods with either variable numbers of arguments or union arguments.

NSInvocation不支持对具有可变数量的参数或联合参数的方法的调用。

If there is an alternate version of the method that takes a va_list and all your parameters are object pointers you might be able to fake something up like in my answer over here: fake va_list in ARC

如果该方法有另一个版本,它接受va_list,并且所有参数都是对象指针,那么您可能可以伪造一些东西,比如我这里的答案:用圆弧伪造va_list

#1


5  

NSInvocation does not support variadic methods, so this will not be possible. (Reference: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html )

NSInvocation不支持可变的方法,所以这是不可能的。(参考:https://developer.apple.com/library/mac/文档/可可/引用/基础/类/ NSInvocation_Class /引用/ Reference.html)

NSInvocation does not support invocations of methods with either variable numbers of arguments or union arguments.

NSInvocation不支持对具有可变数量的参数或联合参数的方法的调用。

If there is an alternate version of the method that takes a va_list and all your parameters are object pointers you might be able to fake something up like in my answer over here: fake va_list in ARC

如果该方法有另一个版本,它接受va_list,并且所有参数都是对象指针,那么您可能可以伪造一些东西,比如我这里的答案:用圆弧伪造va_list