【发布时间】:2012-02-29 16:09:51
【问题描述】:
有没有比这更好的方法来初始化objective-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 */
}
【问题讨论】:
-
如果稍后更改“a”或“b”的值,是否会在添加到模板的所有数组中也发生更改?
-
@iOS 开发者 NSNumber 是不可变对象,其值无法更改
-
@xlc0212 非常感谢!但是如果数组填充了可变对象,它会在添加到模板的所有数组中发生变化,对吧?
标签: objective-c nsmutablearray nsarray