0
votes

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 */
}
3
If you change later the value of "a" or "b", will it also change in all the arrays added to template? - The dude
@iOS developer NSNumber is immutable object which value cannot be changed - Bryan Chen
@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

3 Answers

0
votes

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];
}
0
votes

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]];
}
0
votes

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