0

I have an NSArray arr. It has a bunch of NSNumber objects. I'm trying to calculate statistics analysis on the array using GNU's GSL. GSL takes parameters as C-style arrays.

Is there any mechanism that can, for example, run 'intValue' on all of the objects in a NSArray object, and convert the results that to a C-style array?

I don't really want to copy the contents of the NSArray to a C-style array, as it's a waste of space and cycles, so I'm looking for an alternative.

4

2 に答える 2

4

あなたが説明しているメカニズム — intValueNSArray 内のすべてのオブジェクトで実行され、C スタイルの配列を提供する — は、「スペースとサイクルの無駄」と説明したものとまったく同じようです。また、C スタイルの int 配列が必要な場合にこれを行う唯一の実際の方法です。私が考えることができる最善のアプローチ:

int *c_array = malloc(sizeof(int) * [yourArray count]);
[yourArray enumerateObjectsWithOptions:NSEnumerationConcurrent 
                            usingBlock:^(id number, NSUInteger index, BOOL *unused) {
    c_array[index] = [number intValue];
}];
于 2011-09-05T21:48:27.417 に答える
0

これを試して:

id *numArray = calloc(sizeof(id), yourArray.count);
[yourArray getObjects: numArray range: NSMakeRange(0, yourArray.count)];

これにより、NSNumbers の C 配列が得られます。あなたにintを与える代替:

int *numArray = calloc(sizeof(int), yourArray.count);
for (int i = 0; i< yourArray.count; i++)
    numArray[i] = [[yourArray objectAtIndex: i] intValue];

int の C 配列を直接返すように yourArray に指示する方法はありません。NSArray には、ID であり、適切なタイミングで保持および解放する必要があることを除いて、その内容の概念はありません。最初の例のように、最大​​で ID の C 配列を返すことができます。

おそらく、int (または float や double など) を含む独自の単純な配列クラスを内部 C 配列に直接記述できますが、このためのストック クラスはありません。

于 2011-09-05T22:32:34.943 に答える