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 */
}
4
  • If you change later the value of "a" or "b", will it also change in all the arrays added to template? Commented Feb 8, 2012 at 8:36
  • 2
    @iOS developer NSNumber is immutable object which value cannot be changed Commented Feb 8, 2012 at 10:09
  • different options stackoverflow.com/questions/638129/… Commented Feb 8, 2012 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? Commented Feb 8, 2012 at 16:33

3 Answers 3

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];
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.