从 NSArray 对象获取 C 风格数组

发布于 2024-12-02 23:26:40 字数 228 浏览 5 评论 0原文

我有一个 NSArray arr。它有一堆 NSNumber 对象。我正在尝试使用 GNU 的 GSL 对数组进行统计分析。 GSL 将参数视为 C 样式数组。

例如,是否有任何机制可以对 NSArray 对象中的所有对象运行“intValue”,并将结果转换为 C 样式数组?

我真的不想将 NSArray 的内容复制到 C 样式数组,因为它浪费空间和周期,所以我正在寻找替代方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2024-12-09 23:26:40

您所描述的机制 - 在 NSArray 中的所有对象上运行 intValue 并给出一个 C 样式数组 - 似乎与您所描述的“浪费空间和周期”完全相同。如果您需要 C 风格的整数数组,这也是唯一真正的方法。我能想到的最佳方法:

int *c_array = malloc(sizeof(int) * [yourArray count]);
[yourArray enumerateObjectsWithOptions:NSEnumerationConcurrent 
                            usingBlock:^(id number, NSUInteger index, BOOL *unused) {
    c_array[index] = [number intValue];
}];

The mechanism you're describing — run intValue on all the objects in the NSArray and give a C-style array — seems to be exactly the same thing you describe as "a waste of space and cycles." It's also the only real way to do this if you need a C-style array of ints. Best approach I can think of:

int *c_array = malloc(sizeof(int) * [yourArray count]);
[yourArray enumerateObjectsWithOptions:NSEnumerationConcurrent 
                            usingBlock:^(id number, NSUInteger index, BOOL *unused) {
    c_array[index] = [number intValue];
}];
信愁 2024-12-09 23:26:40

试试这个:

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

这会给你一个 NSNumbers 的 C 数组。为您提供整数的替代方案:

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

无法告诉 yourArray 直接返回整数的 C 数组。 NSArray 对它所拥有的内容没有任何概念,除了它们是 id,并且必须在正确的时间保留和释放。它最多可以返回 ids 的 C 数组,如我的第一个示例中所示。

您可能可以在内部 C 数组中直接编写自己的简单数组类,其中包含整数(或浮点数或双精度数等),但没有为此提供的库存类。

Try this:

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

That gives you a C-array of NSNumbers. An alternative that gives you ints:

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

There is no way to tell yourArray to return a C-array of ints directly. NSArray has no concept of the contents it has, except that they are ids, and must be retained and released at the right times. It can at most return a C-array of ids, as in my first example.

You could probably write your own simple array class that contains ints (or floats or doubles, etc.) directly, in an internal C array, but there is no stock class for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文