简体   繁体   English

使用NSInvocation setArgument:从NSArray返回值

[英]Using NSInvocation setArgument: with value returned from NSArray

I have the following issue. 我有以下问题。

I have an NSArray of values that I need to loop across and set the contents as the respective arguments for an NSInvocation object. 我有一个NSArray值,我需要遍历并将值设置为NSInvocation对象的相应参数。 Consider the following code: 考虑以下代码:

NSArray *args = @[@"test"];

later I would like to do something like: 后来我想做些类似的事情:

for ( i = 0; i < [args count]; i++)
{
    [invocationObj setArgument: [args objectAtIndex: i] atIndex: i+2];
}

but this doesn't work. 但这不起作用。 It gives the following warning: 它给出以下警告:

Implicit conversion of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast

The invocation object is dynamic (for both target/selector and args) and so I don't know the contents of the args array when i am setting the arguments of the respective invocationObj. 调用对象是动态的(对于目标/选择器和args),因此当我设置各个invocationObj的参数时,我不知道args数组的内容。 Is there any way I can achieve this? 有什么办法可以实现?

You need to pass a pointer location into setArgument:atIndex: . 您需要将指针位置传递给setArgument:atIndex: Specifically, this part from the docs 具体来说,这部分来自文档

When the argument value is an object, pass a pointer to the variable (or memory) from which the object should be copied: 当参数值是一个对象时,将指针传递到应该从中复制对象的变量(或内存):

So try this... 所以试试这个...

id argument = [args objectAtIndex: i];
[invocationObj setArgument:&argument atIndex: i+2];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM