NSMutableArray 和 NSArray 初始化
在 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 */
}
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 */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在你的数组中找不到规律性。至少你可以将一些代码包装到循环中
I cann't find regularity in your arrays. At least you can wrap some code into cycle
假设你想创建一个 NSNumber 的二维数组
assume you want to make a 2d array of NSNumber
使用 nsdictionary 保存数字,然后将字典存储在 nsarray 中
Use nsdictionary to hold numbers and then store the dictionary in nsarray