gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:22:46 29 4
gpt4 key购买 nike

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

我可以这样直接调用

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

如果我使用 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];

最佳答案

NSInvocation 不支持可变参数方法,所以这是不可能的。 (引用: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.

如果有一个采用va_list 的方法的替代版本,并且您的所有参数都是对象指针,您也许可以伪造一些东西,就像我在这里的回答:fake va_list in ARC

关于ios - 在 NSInvocation 中为具有可变参数的方法传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17995544/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com