简体   繁体   English

使用ARC从NSInvocation获取对象数组参数

[英]Get object array argument from NSInvocation with ARC

I have a method with the following signature: 我有以下签名的方法:

- (void)takeIntsAndRecieveIntsAsync:(MyInt *__strong [])
                         completion:(void (^)(MyInt * __strong response[]))success;

I had a couple of questions: 我有几个问题:

1. How do I retrieve the argument using NSInvocation in this method? 1.如何在此方法中使用NSInvocation检索参数?

- (void)forwardInvocation:(NSInvocation *)invocation

I tried the following but I don't get the correct value: 我尝试了以下操作,但没有获得正确的值:

__unsafe_unretained MyInt *a[2];
[invocation getArgument:(void *) &a atIndex:index];

I can have an array of n int objects, so I should not hardcode 2. How do I determine the size of the object array at runtime? 我可以有n个int对象的数组,所以我不应该硬编码2。如何在运行时确定对象数组的大小? Do I need a sentinel value in the array to determine the bounds? 我是否需要数组中的哨兵值来确定范围?

2. Is the __strong qualifier correct for both the input parameter and the block parameter? 2. __strong限定词对输入参数和块参数均正确吗? Please note that the block is asynchronous and would execute at a later time. 请注意,该块是异步的,将在以后执行。

Your method does not take an array parameter -- it takes a pointer parameter. 您的方法不采用数组参数,而是采用指针参数。

In C, it's not possible to have a parameter of array type. 在C语言中,不可能有数组类型的参数。 If you write a parameter of type "array of X", it is automatically changed to type "pointer to X". 如果编写的参数类型为“ X的数组”,则参数将自动更改为“ X的指针”。

Objective-C methods are C functions, but the story is a little different. Objective-C方法是C函数,但是情况有所不同。 In recent versions of Xcode, it appears that you can have method parameters of array type (although this does not seem to be documented anywhere). 在最新版本的Xcode中,似乎可以使用数组类型的方法参数(尽管似乎没有在任何地方进行记录)。 But it must be a concrete array type, ie with a length hardcoded at compile-time: 但是它必须是一个具体的数组类型,即在编译时具有一个硬编码的长度:

- (void)takeIntsAndRecieveIntsAsync:(MyInt *__strong [5]);

When it's an incomplete array type (eg MyInt *__strong [] ), it is still automatically adjusted to a pointer (eg MyInt *__strong * ), the value being a pointer to the first element of the array. 当它是不完整的数组类型时(例如MyInt *__strong [] ),它仍会自动调整为指针(例如MyInt *__strong * ),该值是指向数组第一个元素的指针。

  1. In C (and thus Objective-C), it is not possible to know, given a pointer to the first element of an array, how many elements this "array" has. 在C语言中(因此在Objective-C中),在给定指向数组第一个元素的指针的情况下,无法知道此“数组”具有多少个元素。 You must get this information separately somehow (whether it be a separate length parameter, the length and pointer packaged together in some kind of structure, a "sentinel" value in the array, etc.). 您必须以某种方式分别获取此信息(无论它是一个单独的length参数,以某种结构打包在一起的length和指针,数组中的“ sentinel”值,等等)。

  2. It should match the type of the array you're passing the pointer to the first element of. 它应该与您将指针传递给第一个元素的数组的类型匹配。 If it's an array of __strong , then it needs to be pointer to __strong , etc. If your operation is asynchronous, then you probably need to copy the array (which requires you to figure out the solution to question 1, how to figure out the length of the array), so that your asynchronous operation will have strong references to those MyInt objects. 如果它是__strong的数组,则它需要指向__strong指针,等等。如果您的操作是异步的,则可能需要复制该数组(这要求您找出问题1的解决方案,如何找出问题的解决方案)。数组的长度),以便您的异步操作对这些MyInt对象具有强大的引用。 Thus your copy of the array should contain __strong pointers. 因此,您的数组副本应包含__strong指针。

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

相关问题 使用ARC从NSInvocation获取块参数 - Get block argument from NSInvocation with ARC 尝试从NSInvocation获取NSString类型参数时发出警告 - Warning when trying to get NSString type argument from NSInvocation Obj-C:如何从NSInvocation获取并调用块参数 - 在iOS上存根Twitter帐户 - Obj-C: How to get and call a block argument from NSInvocation - stubbing Twitter account on iOS 如何使用将指向对象的指针作为参数的方法创建NSInvocation对象 - How to create an NSInvocation object using a method that takes a pointer to an object as an argument 弧。 对象从数组指向零 - ARC. Object from array points to nil 确定来自NSInvocation getArgument的if(void *)指针是对象还是原始指针 - Determine if (void *) pointer from NSInvocation getArgument is object or primitive 带有ARC的Objective C中的对象数组 - Object Array in Objective C with ARC Obj-C ARC:如何从数组/集中删除对象,然后将其作为自动释放的对象返回? - Obj-C ARC: How to remove an object from an array/set and then return it as an autoreleased object? 使用NSInvocation setArgument:从NSArray返回值 - Using NSInvocation setArgument: with value returned from NSArray NSInvocation是否应该将self作为2d索引参数传递? - Should NSInvocation passes self as 2d indice argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM