How to Build a MultiDimensional Array of Objects in Objective C

multidimensional-array, objective-c

Solution

I'd suggest you use a C-array, as an `NSArray` doesn't support multiple dimensions. You could declare the array you described like this:

NSString *stringArray[16][3];

Setting and accessing any string of this array is quite straight-forward:

stringArray[7][1] = @"Stringstringstring";

NSString *string = stringArray[3][0];

However, you could use an `NSArray` (or `NSMutableArray`), but that would be a bit less elegant:

NSArray *stringArray = [NSArray arrayWithObjects:
                        [NSMutableArray array],
                        [NSMutableArray array],
                        [NSMutableArray array], nil];

Those three `NSMutableArray`s would be the three columns of your two-dimensional array.

Edit

Using an `NSArray`, it might be easier to use a loop to fill it:

NSMutableArray *stringArray = [NSMutableArray array];

for (int column = 0; column < 3; column++)
{
    NSMutableArray *columnArray = [NSMutableArray array];

    for (int row = 0; row < 16; row++)
        [columnArray addObject:[NSString stringWithFormat:@"Row %i, column %i", row, column]];

    [stringArray addObject:columnArray];
}

Problem

Possible Duplicate: How do I create a multidimensional array? I'm new to programming and objective C, so although I have found a few questions on here that discuss multidimensional arrays, I'm not quite getting what I need to do in order to build and use my own. I need to make an array that has 16 rows and 3 columns. The array needs to accept string objects. I do not know how to create this, fill it, or access its contents. Would anyone be kind enough to break it down for me?

Original source