如何从 Objective-C 中的方法返回 C 数组? [英] How to return a C-array from method in Objective-C?

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

问题描述

我有一个返回变量的函数,我想知道如何返回一个数组,问题是它不是一个 NSArray,它只是一个像这样的普通 C 数组...

I have a function that returns a variable and I want to know how to return an array the issue is it isn't an NSArray it is just an average C array like this...

-(b2Fixture*) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape
{
    BodyDef *so = [shapeObjects objectForKey:shape];
    assert(so);
       FixtureDef *fix = so->fixtures;
    int count = -1;
    b2Fixture *Fixi[4];
    while(fix)
    {

        count++;
        NSLog(@"count = %d",count);
        Fixi[count]= body->CreateFixture(&fix->fixture);
        if (Fixi[count]!=0) {
            NSLog(@"Fixi %d is not 0",count);
        }
        if (body->CreateFixture(&fix->fixture)!=0) {
            NSLog(@"body %d is not 0",count);
        }

        fix = fix->next;

    }

    return *Fixi;

}

如果你看到一些你不知道的变量类型,那是因为我正在使用 cocos2d 框架制作游戏,但我返回的是 b2Fixture 的变量...此代码编译但只保存第一个块的值数组fixi [0]"不是我想要传递的整个数组

If you see some variable types you don't know it's because I'm using cocos2d framework to make a game but I'm returning a variable of b2Fixture... This code compiles however only saves the value of the first block of the array "fixi[0]" not the whole array like I want to pass

任何帮助 :) 谢谢你

anyhelp :) thankyou

推荐答案

不能返回本地数组.您需要进行某种动态分配或使用诸如将数组放入结构中的技巧.

You can't return a local array. You'll need to do some kind of dynamic allocation or pull a trick like having the array inside a structure.

这里有一个链接到一篇深入的文章应该能帮到你.

Here is a link to an in-depth article that should help you out.

这篇关于如何从 Objective-C 中的方法返回 C 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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