是否有更好的方法來初始化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 */
}
字典如果您稍後更改「a」或「b」的值,是否也會在添加到模板的所有數組中更改? – 2012-02-08 08:36:09
@iOS開發者NSNumber是不可改變的對象,其值不能改變 – 2012-02-08 10:09:37
不同的選項http://stackoverflow.com/questions/638129/how-to-declare-a-two-dimensional-array-of-string-type-in- objective-c – 2012-02-08 10:20:58