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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to