[英]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 *
),该值是指向数组第一个元素的指针。
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”值,等等)。
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.