How to return a C-array from method in Objective-C?

一世执手 提交于 2020-01-04 04:11:19

问题


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


回答1:


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.




回答2:


In general returning C arrays by value is a bad idea, as arrays can be very large. Objective-C arrays are by-reference types - they are dynamically allocated and a reference, which is small, is what is passed around. You can dynamically allocate C arrays as well, using one of the malloc family for allocation and free for deallocation.

You can pass C structures around by value, and this is common, as in general structures tend to be small (or smallish anyway).

Now in your case you are using a small array, it has just 4 elements. If you consider passing these 4 values around by value is reasonable and a good fit for your design then you can do so simply by embedding the C array in a C structure:

typedef struct
{
    b2Fixture *elements[4];
} b2FixtureArray;

...

-(b2FixtureArray) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape
{
    BodyDef *so = [shapeObjects objectForKey:shape];
    assert(so);
    FixtureDef *fix = so->fixtures;
    int count = -1;
    b2FixtureArray Fixi;
    while(fix)
    {   
        count++;
        NSLog(@"count = %d", count);
        Fixi.elements[count]= body->CreateFixture(&fix->fixture);
        if (Fixi.elements[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;    
}

...

// sample call outline
b2FixtureArray result = [self addFixturesToBody...]

Whether this standard C "trick" for passing arrays by value is appropriate for your case you'll have to decide.

Note: If b2fixture is an Objective-C object make sure you understand the memory management implications of having a C array of objects references depending on the memory management model (MRC, ARC, GC) you are using.




回答3:


If you need to design function or method that has to return a fixed or limited size array, one possibility is to pass a pointer to the result array to the function or method as a parameter. Then the caller can take care of allocating space, or just use a local or instance variable array. You might want the called function to sanity check that the array parameter isn't NULL before using the array.



来源:https://stackoverflow.com/questions/9651465/how-to-return-a-c-array-from-method-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!