Re: Two Dimensional Array
Re: Two Dimensional Array
- Subject: Re: Two Dimensional Array
- From: Graff <email@hidden>
- Date: Fri, 12 Sep 2008 15:24:35 -0400
On Sep 12, 2008, at 5:05 AM, Jordon Hirshon wrote:
I'm new to Objective-C. Can someone suggest an approach to building
a two dimensional array of integers?
You can either make an NSArray that contains other NSArrays or just
use a c-style multidimensional array.
example of using an NSArray:
int myValue = 10;
// create the 2-D array
NSArray *twoD = [NSArray arrayWithObjects:
[NSMutableArray new],
[NSMutableArray new],
[NSMutableArray new],
[NSMutableArray new], nil];
// store the value as an NSNumber
[[twoD objectAtIndex:2] addObject:[NSNumber numberWithInt:myValue]];
// retrieve the value
int retrievedValue = [[[twoD objectAtIndex:2] objectAtIndex:0]
intValue];
example of using a c-array:
int myValue = 10;
// create the 2-D array
int twoD[4][6];
// store the value as an NSNumber
twoD[2][0] = myValue;
// retrieve the value
int retrievedValue = twoD[2][0];
There are advantages and disadvantages to each method. If you poke
around the internet you can find some pre-made objective-c classes
that handle multi-dimensional arrays for you but for a simple 2-D
array it's probably best to stick with something similar to what I've
outlined here.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden