简体   繁体   中英

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. 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. Is there any way I can achieve this?

You need to pass a pointer location into 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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