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

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

问题描述

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



- (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]的第一个块的值不是像I

解决方案

您可以' t返回一个局部数组。你需要做一些动态分配,或者拉一个把数组放在结构中的技巧。



这里有一个链接,可帮助您深入了解。


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;

}

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天全站免登陆