0

Is there a better way to initialize a 2 dimensional array in objective-c than this:

NSNumber *a = [[NSNumber alloc] initWithInt:0];
NSNumber *b = [[NSNumber alloc] initWithInt:1];
NSMutableArray *template = [[NSMutableArray alloc] initWithCapacity:16];
switch (myInt) {
    case 0:
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, a, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, a, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, a, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, a, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        break;
/* more */
}
Justin808
  • 20,859
  • 46
  • 160
  • 265
  • If you change later the value of "a" or "b", will it also change in all the arrays added to template? – The dude Feb 08 '12 at 08:36
  • 2
    @iOS developer NSNumber is immutable object which value cannot be changed – Bryan Chen Feb 08 '12 at 10:09
  • different options http://stackoverflow.com/questions/638129/how-to-declare-a-two-dimensional-array-of-string-type-in-objective-c – Girish Kolari Feb 08 '12 at 10:20
  • @xlc0212 thank you so much! But in case array was populated with mutable objects, it would change in all the arrays added to template, right? – The dude Feb 08 '12 at 16:33

3 Answers3

0

I cann't find regularity in your arrays. At least you can wrap some code into cycle

id obj = [[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil];

for (int i = 0; i<9; ++i){
    [template addObject: obj];
}
beryllium
  • 29,669
  • 15
  • 106
  • 125
0

assume you want to make a 2d array of NSNumber

const int templateContent[][8] = { // put your data to this array
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 0, 1, 1, 1, 1, 1, 1,
    // more...
};
const int dim = sizeof(templateContent)/sizeof(int[8]);
NSNumber *numbers[2]; // assume your only need two numbers
for (int i = 0; i < sizeof(numbers)/sizeof(id); i++) {
    numbers[i] = [NSNumber numberWithInt:i];
}
NSMutableArray *template = [[NSMutableArray alloc] initWithCapacity:16];
for (int i = 0; i < dim; i++) {
    [template addObject:[NSArray arrayWithObjects:
                         numbers[templateContent[i][0]], 
                         numbers[templateContent[i][1]], 
                         numbers[templateContent[i][2]], 
                         numbers[templateContent[i][3]], 
                         numbers[templateContent[i][4]], 
                         numbers[templateContent[i][5]], 
                         numbers[templateContent[i][6]], 
                         numbers[templateContent[i][7]], nil]];
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
0

Use nsdictionary to hold numbers and then store the dictionary in nsarray

Piyush Kashyap
  • 1,965
  • 1
  • 14
  • 16