NSArray二维数组的定义

初始化使用这个方法[[NSArray alloc] initWithObjects:……,nil];

1
2
3
4
5
6
//OC
NSArray *arr = [[NSArray alloc] initWithObjects:
[[NSArray alloc] initWithObjects: @"1",@"1",nil],
[[NSArray alloc] initWithObjects: @"2",@"2",@"2",nil],
[[NSArray alloc] initWithObjects: @"3",@"3",@"3",@"3",nil],
,nil];

如果要初始化1000项,可以这样:

1
2
3
4
5
//OC
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:1];
for (int i = 0; i<1000; i++) {
[arr addObject:@"德玛西亚"];
}

当然换成swift就超级简单了:

1
2
3
4
5
6
//swift
var arr = [
["1"],
["2","2"],
["3","3","3"]
]

初始化1000项:

1
2
//swift
var arr = [String](count: 1000, repeatedValue: "德玛西亚")

多维数组:

1
2
//swift
var arr = [[String]](count: 1000, repeatedValue: [String](count: 10, repeatedValue: "德玛西亚"))

好像每个人都是准备OC转swift,是不是只有我这个奇葩放掉了swift来重新学OC

打赏