NSMutableArray и NSArray инициализация - PullRequest
0 голосов
/ 08 февраля 2012

Есть ли лучший способ инициализировать двумерный массив в target-c, чем этот:

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 ]

0 голосов
/ 08 февраля 2012

предполагается, что вы хотите создать 2-мерный массив из 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 голосов
/ 08 февраля 2012

Используйте nsdictionary для хранения чисел, а затем сохраните словарь в nsarray

0 голосов
/ 08 февраля 2012

Я не могу найти регулярность в ваших массивах.По крайней мере, вы можете заключить код в цикл

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

for (int i = 0; i<9; ++i){
    [template addObject: obj];
}
...