Objective-C的NSArray的到c数组 [英] objective-c nsarray to c array

查看:217
本文介绍了Objective-C的NSArray的到c数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我什至不知道怎么问,因为我位于C是一个完整的新手,指针和类似的东西。有一个接受一个参数的函数:的char ** ARG 。如果我写这样的说法,像这样:

Sorry, I'm not even sure how to ask, since I'm a complete newbie at C, pointers and stuff like that. There's a function that accepts an argument: char **arg. If I write that argument like so:

char *cargs[] = {"blah", NULL};

和它传递给函数:

function(cargs);

它的工作原理。但是...我有一个的NSArray NSString的和我需要让这个数组从<$ C OUT值$ C>的NSArray 。我想这应该是创建相同的元素计数的C数组为的NSArray 的问题与复制的字符串, cStringUsingEncoding 。但老实说,我不知道如何做到这一点,因为我感到困惑与所有这些指针和这样的。任何帮助将是AP preciated。

it works. but ... I have an NSArray of NSStrings and I need to make this array out of values from NSArray. I figured it should be a matter of creating a C array of the same element count as NSArray and copy the strings, converting them with cStringUsingEncoding. But I honestly have no idea how to do this, since I get confused with all those pointers and such. Any help would be appreciated.

推荐答案

好了,粗糙的步骤可以是:

Well, the rough steps can be:


  1. 使用计数的NSArray的方法来知道有多少NSString的在NSArray中的存在。

  1. use count method of NSArray to know how many NSStrings are there in the NSArray.

使用malloc为cargs分配内存,这样的事情

use malloc to allocate memory for cargs, something like this

char **cargs = (char **) malloc(sizeof(char *) * count);

你的榜样,你可能需要一个NULL,这将是在cargs年底更大的空间。

by your example, you may need to one more room for NULL which will be at the end of cargs.

使用循环和 objectAtIndex:NSArray中的的走出NSString的,像
* NSString的NSString的= [阵列objectAtIndex:指数];

use a loop and objectAtIndex: of NSArray to get out the NSStrings, like NSString *nsstring = [array objectAtIndex:index];

使用方法 cStringUsingEncoding:,以获得C-串出,更使复印件

use method cStringUsingEncoding: to get the c-string out, better make a copy

把这些C-字符串指针在cargs

put these c-string pointers in cargs

通过cargs给你的函数,需要清洁,免费的东西。

pass cargs to your function, clean and free things needed to.

这是一个很大的工作。 原因c和OBJ-C材料的组合。而大量的人工malloc和free,凌乱的东西。你不能避免呢?

It's a lot of work. 'Cause the mix of c and obj-c stuff. And a lot of manual malloc and free , messy stuff. Can't you avoid it?

- 加样code -

--add sample code--

我不太确定你的真正意图是什么。希望这会有所帮助。

I'm not quite sure what your real intent is. Hope this will help.

void func(char **arg)
{
    int i;
    for(i = 0; arg[i] != NULL; i++) {
        printf("%d=%s\n", i, arg[i]);
}
}
int main(int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *s1 = [NSString stringWithString:@"first"];
    NSString *s2 = [NSString stringWithString:@"second"];
    NSString *s3 = [NSString stringWithString:@"third"];

    NSArray *array = [NSArray arrayWithObjects: s1, s2, s3, nil];
    //by now, we have an NSArray of three NSStrings

    int count = [array count];
    char **cargs = (char **) malloc(sizeof(char *) * (count + 1));
    //cargs is a pointer to 4 pointers to char

    int i;
    for(i = 0; i < count; i++) {
        NSString *s = [array objectAtIndex:i];//get a NSString
        const char *cstr = [s cStringUsingEncoding:NSUTF8StringEncoding];//get cstring
        int len = strlen(cstr);//get its length
        char *cstr_copy = (char *) malloc(sizeof(char) * (len + 1));//allocate memory, + 1 for ending '\0'
        strcpy(cstr_copy, cstr);//make a copy
        cargs[i] = cstr_copy;//put the point in cargs
    }
    cargs[i] = NULL;

    func(cargs);//call the function to do something

    for(i = 0; i < count; i++) {
        free(cargs[i]);
    }
    free(cargs);

    [pool drain];
    return 0;
}

这篇关于Objective-C的NSArray的到c数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆