IronMan
IronMan

Reputation: 1505

Passing multiple arguments in NSInvocation for a method with variable arguments

I have a method which take variable arguments something like this, the arguments end with 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

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];

Upvotes: 3

Views: 1023

Answers (1)

ipmcc
ipmcc

Reputation: 29946

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 does not support invocations of methods with either variable numbers of arguments or union arguments.

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

Upvotes: 5

Related Questions