0

I have a quick question:

How can I return a C-style 2 dimensional array from an Objective C method, containing pointers to various objects that I can address using myArray[x][y].someProperty or [myArray[x][y] someMethodCall:myArgument]? I'm guessing that something like NSData would need to be used to "package" the return data, which would be unpackaged on the other end.

The objects that need to be addressed like so are stored internally inside the Objective C class as a 1 dimensional NSMutableArray, which has a capacity equal to width*height. I'm aware that some pointer magic is going to have to happen here to translate between the C-style 2D array and the actual 1 dimensional NSMutableArray.

Is this possible?

Cheers, -Keven Tipping

1
  • 1
    It might be worth recommending that, instead of trying to return a 2D array, you might ask the user to pass a 2D buffer into the function, and just copy your data into that buffer (i.e. change x = func(); to func(&x);, though with arrays the & is not exactly correct depending on how you declare it). Commented Aug 31, 2011 at 2:25

4 Answers 4

3

A C (or Objective-C) function cannot return an array. However, it can return a struct containing an array:

struct array {
    int v[5][5];
};

struct array f() {
    struct array a;
    // fill 'a' here
    a.v[0][0] = 1;
    ...
    return a;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you really need to be able to index like [x][y] and you care about memory management, perhaps you could create a wrapper class that, upon initialization, mallocs an id** (or the appropriate class, if you know ahead of time) of length x_max, and fills each x with a malloc of length y_max. Then, obviously, it all needs to be released in the dealloc method. You could call that with wrapper.array[x][y]. You could even store the lengths so you can ask the wrapper how big its array is.

One thing: I'm not sure how well malloc plays with apple's [Class alloc]. Might want to check that first.

Comments

0

There is no problem declaring a C-style multi-dimensional array of Obj-C objects and access

n = myArray[x][y].myProperty;

or do

[myArray[x][y] someMethod: someArgument];

It is just like using one variable, instead of an element of an array. But that is a simple C style array of object references, and not an object.

You want something different. Your array is an array inside a class and you want to access the elements. To access the elements, do as with single-dimensional arrays, but e.g.

a = [my2DimArray objectAtIndexX: x indexY: y];

and

[my2DimArray replaceObjectAtIndexX: x indexY: y withObject: myObject];

or

[my2DimArray addRow: myNSArray];

and

RV2DimArray *my2DimArray = [RV2DimArray arrayWithX: 17 Y: 39];

Or, if you are going for more:

RVMultiDimArray *a = [[RVMultiDimArray alloc] initWithDimensions: 4, 4, 8, 0];

etc...

Comments

0

You can set your obj-c function to return an NSArray. And inside that NSArray is an NSArray. So it's like returning an array of NSArray.
yourNSArray = [self getArray]; //returns an array of array

After you have your NSArray, you can then transfer it to a two-dimensional C-style array:
c[0][0] = [[[yourNSArray objectAtIndex:0] objectAtIndex:0] intValue];
c[0][1] = [[[yourNSArray objectAtIndex:0] objectAtIndex:1] intValue];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.