gpt4 book ai didi

ios - 使用 NSInvocation setArgument : with value returned from NSArray

转载 作者:行者123 更新时间:2023-11-28 20:12:41 35 4
gpt4 key购买 nike

我有以下问题。

我有一个 NSArray 值,我需要遍历这些值并将其内容设置为 NSInvocation 对象的相应参数。考虑以下代码:

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

稍后我想做这样的事情:

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

但这行不通。它给出以下警告:

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

调用对象是动态的(对于目标/选择器和 args),因此当我设置相应 invocationObj 的参数时,我不知道 args 数组的内容。有什么办法可以实现这一目标吗?

最佳答案

您需要将指针位置传递到 setArgument:atIndex:。具体来说,这部分来自文档

When the argument value is an object, pass a pointer to the variable (or memory) from which the object should be copied:

所以试试这个...

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

关于ios - 使用 NSInvocation setArgument : with value returned from NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19545654/

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