PDA

View Full Version : 2D Arrays in Cocoa


Niemann
2002.05.05, 03:34 PM
How would I go about doing a 2D array in Cocoa. In C I can just do something along the lines of 'int map[10][10];' but Cocoa doesnt seem to like that. My first idea was to do NSMutableArray, but I cant do a 'int map[2][2] = {{ 1, 0 },{ 0, 1 }};' with an NSMutableArray...or can I?

swcrissman
2002.05.05, 03:52 PM
You can create a NSMutableArray and fill it with NSMutableArrays, if you want, as NSMutableArrays are objects too. Understand?

Spencer

peacha
2002.05.05, 04:12 PM
This issue has come up more than once over the net... hope that apple addresses this lack soon (and maybe adds an array for non objects).
Also, why there's no NS counterpart of CFTree :confused:

Pazolli
2002.05.05, 06:47 PM
If you want non-objects as arrays, why not use the C-equivalent? That is, for example, use int map[12][12]. The only reason I can think of not to, is that there is no range checking. That is you can write map[testNum][testNum] = 1; where testNum = -1 with no problems compiling but lots of trouble later. To overcome this problem you can either be really careful or make the array a private data member and do all editing of the array through a method that has range checking. That said, if Objective way to deal with non-object type arrays is essential you should be able to easily write a class for this yourself.

Johan
2002.05.06, 05:47 AM
Why do you need to use 2d arrays anyways? I don't think I've ever used them.

eg.


int size = 10;
int array[size][size];

int get = array[x][y];


equals:


int size = 10;
int array[size*size];

int get = array[x + y*size];


/JP

jefftkd
2002.05.06, 05:11 PM
If you want dynamic 2D arrays, NSMutableArray can be way too slow for what you want. Two options:

1. Just use malloc() and free() -- simple, but damn fast :)

2. Consider using NSMutableArray with NSMutableData in each index if all your data types will be the same:

NSMutableArray *buildArray() {
int sampleData[] = { 1,2,3 };
int len = sizeof(sampleData)/sizeof(int);

/* Do this for each primary index in the array */
NSMutableData *data = [NSMutableData dataWithBytes:sampleData length:len];

return [NSMutableArray arrayWithObject:data];
}

Now if you want to adjust a data index or increase the length, it is very simple (example):

void appendNumberToIndex(NSMutableArray *array,int index,int n) {
[[array objectAtIndex:index] appendBytes:&n length:sizeof(int)];
}

And to retrieve a value:

int getValueAtIndex(NSMutableArray *array,int primary,int index) {
NSData *data;

if (primary >= [array count]) return 0;
else data = [array objectAtIndex: primary];

if (index > [data length] / sizeof(int)) return 0;
else return (int*)[data bytes][index];
}

Hope this make sense and helps!

Jeff :cool:

GoodDoug
2002.05.09, 08:01 PM
JPersson's use of 2d indexing is right on the money. It is almost always faster (especially in the NSMutableArray case).

What I would doô:
Create my own container class lets say we call it My2DArray
I would offer standard create and delete methods, basing them on the currently available container classes.
Adding:
- (id) initWithRows:(int) inNumRows columns:(int) inNumColumns
I would then create my own accessor methods like
- (void) setObject:(id) inObject atXIndex:(int) inX yIndex:(int) inY
- (id) objectAtXIndex:(int) inX yIndex:(int) inY
- (void) setObject:(id) inObject inRow:(int) inRow column:(int) inColumn
- (id) objectInRow:(int) inRow column:(int) inColumn

Then, in the implementation of those, I would use 2d indexing to find the object

- (id) objectAtXIndex:(int) inX yIndex:(int) inY
{
if (inX < [self rowCount] && inY < [self columnCount])
return [internalArray objectAtIndex:(inX + (inY * [self rowCount]))];
else
return nil;
}


Before I _really_ used it, I would create a little test program to make sure it was doing what I expected:

My2DArray* test = [[My2DArray alloc] initWithRows:4 columns:4];
[test setObject:@"Hello World!" inRow:2 column:3];
NSLog(@"temp = %@", [test description]);


This also implies that you've overriden "description" to return a string useful in debugging. It should return a string with the rows, columns and the description of the internalArray.

Anyways, that's what I'd do.

Niemann
2002.05.09, 09:17 PM
Thanks guys. Ive already got it up and running without flaws. Thanks everyone.

Pazolli
2002.05.11, 01:27 AM
Originally posted by GoodDoug
JPersson's use of 2d indexing is right on the money. It is almost always faster (especially in the NSMutableArray case).

Faster, perhaps. But easier to read and quicker to work with (when coding), maybe not. When you've got CPUs running at thousands of MHz and compilers' optimization techniques continually improving, I think you're more than justified in using 2D/3D/4D... arrays.

GoodDoug
2002.05.13, 06:16 PM
Originally posted by Pazolli


Faster, perhaps. But easier to read and quicker to work with (when coding), maybe not. When you've got CPUs running at thousands of MHz and compilers' optimization techniques continually improving, I think you're more than justified in using 2D/3D/4D... arrays.

CPU speed has nothing to do with this. Typical nD arrays suffer from spatial separation, and speed is based on memory throughput, paging schemes and so forth. Especially in the case of NSMutableArrays... When you consider the overhead of dynamically growing these things (both in memory and time), it can become quite a resource hog.

Also, which do you find easier to read/quicker to work with:


aValue = [[rowArray objectAtIndex:i] objectAtIndex:j];

or

aValue = [my2DArray objectAtRow:i column:j];


I agree with you that the initial investment to get it to work may not always justify the payoff... however, once you create this class, it is easily reused and much simpler to work with in the long run. And, as your arrays get more dynamic and larger, you won't have to contend with locality issues messing up your timing or memory use.

Bindu
2010.04.23, 02:40 AM
I am Creating a 2D array like this and I am implementing that 2D array in my application. But i need to put this 2D array data to a UIPickerView or in UITableView can any one help me with the code .....

Bindu
2010.04.23, 03:30 AM
How to display 2D array data with a UIPickerView or with a UITableView in iphone application ?

I have created a 2D array class and with that i am implementing 2D array in my application. Now i need to display this 2D array data in UIPickerView or in UITableView. Can anyone help me for this...

Thank you for all the help........