2012-02-08 94 views
0

是否有更好的方法來初始化Objective-C的2維數組比這的NSMutableArray NSArray的和初始化

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 */ 
} 
+0

字典如果您稍後更改「a」或「b」的值,是否也會在添加到模板的所有數組中更改? – 2012-02-08 08:36:09

+2

@iOS開發者NSNumber是不可改變的對象,其值不能改變 – 2012-02-08 10:09:37

+0

不同的選項http://stackoverflow.com/questions/638129/how-to-declare-a-two-dimensional-array-of-string-type-in​​- objective-c – 2012-02-08 10:20:58

回答

0

我上無法找到你的陣列的規律性。至少你可以用一些代碼爲週期

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

假設你想的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

使用NSDictionary的持有數量,然後存儲在NSArray中