开发者

How do I return a 2-dimensional C style array from an Objective C method?

开发者 https://www.devze.com 2023-04-01 02:28 出处:网络
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 [myAr

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 l开发者_开发问答ike 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


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


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.


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...


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号