Objective-C nsarray 到 c 数组

发布于 2024-09-06 16:54:21 字数 421 浏览 3 评论 0原文

抱歉,我什至不知道如何问,因为我是 C、指针之类的新手。有一个接受参数的函数:char **arg。如果我像这样编写该参数:

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

并将其传递给函数:

function(cargs);

它会起作用。但是...我有一个 NSStringsNSArray,我需要用 NSArray 中的值来创建这个数组。我认为这应该是创建一个与 NSArray 元素数量相同的 C 数组并复制字符串,使用 cStringUsingEncoding 转换它们的问题。但老实说,我不知道该怎么做,因为我对所有这些指针等感到困惑。任何帮助将不胜感激。

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};

and pass it to the function:

function(cargs);

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.

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

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

发布评论

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

评论(2

吾家有女初长成 2024-09-13 16:54:21

嗯,大致的步骤可以是:

  1. 使用 NSArray 的 count 方法来知道 NSArray 中有多少个 NSString。

  2. 使用malloc为cargs分配内存,类似这样

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

    根据您的示例,您可能需要为 NULL 多一个空间,该空间将位于 cargs 的末尾。

  3. 使用 NSArray 的循环和 objectAtIndex: 来获取 NSString,例如
    NSString *nsstring = [array objectAtIndex:index];

  4. 使用方法cStringUsingEncoding:来获取c字符串,更好地复制

  5. 将这些 C 字符串指针放入 cargs

  6. 将 cargs 传递给您的函数,清理并释放所需的东西。

这是很多工作。因为 c 和 obj-c 的混合。还有很多手动 malloc 和免费的、混乱的东西。你就不能避免吗?

--添加示例代码--

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

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;
}

Well, the rough steps can be:

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

  2. use malloc to allocate memory for cargs, something like this

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

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

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

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

  5. put these c-string pointers in cargs

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

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?

--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;
}
我爱人 2024-09-13 16:54:21

@yehnan,你的答案是伟大 - 特别是因为我认为 const ** cStyle[arrays] 无法动态创建...... (我真的希望有一个地方只有关于 C 指针和变量的基本信息以及废话......我发现最基本的“C 原语”比其他任何东西都更令人困惑...... .)这是一个很好的小例子,“捆绑”到一个函数中......“为了孩子们......

char ** cArrayFromNSArray ( NSArray* array ){
   int i, count = array.count;
   char **cargs = (char**) malloc(sizeof(char*) * (count + 1));
   for(i = 0; i < count; i++) {        //cargs is a pointer to 4 pointers to char
      NSString *s      = array[i];     //get a NSString
      const char *cstr = s.UTF8String; //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;
  return cargs;
}

@yehnan your answer is GREAT - especially because I thought that const ** cStyle[arrays] were NOT able to be created dynamically... (I really wish there was somewhere with just basic information about C pointers and variables and crap... I find the most basic "C primitives" to be far more bewildering than anything else...) Here's your nice little example "bundled up" into a function... "for the children..."

char ** cArrayFromNSArray ( NSArray* array ){
   int i, count = array.count;
   char **cargs = (char**) malloc(sizeof(char*) * (count + 1));
   for(i = 0; i < count; i++) {        //cargs is a pointer to 4 pointers to char
      NSString *s      = array[i];     //get a NSString
      const char *cstr = s.UTF8String; //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;
  return cargs;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文