获取长度
OC:
NSArray *arr = @[@1, @2, @3];
NSLog(@"%tu", arr.count);
输出结果: 2016-04-01 01:19:54.874 test[12086:2209180] 3
swift:
var arr = [1, 2, 3]
print(arr.count)
输出结果:3
判断是否为空
OC:
NSArray *arr = @[];
NSLog(@"result = %d", arr.count != 0);
输出结果: 2016-04-01 01:25:14.252 OCTest[12249:2255010] result = 0
swift:
var arr = [1, 2, 3]
print(arr.isEmpty)
输出结果: false
检索
OC:
NSArray *arr = @[@1, @2, @3];
NSLog(@"%@", arr[0]);
输出结果: 2016-04-01 01:26:46.816 OCTest[12275:2269853] 1
swift:
var arr = [1, 2, 3]
print(arr[0])
输出结果: 1
追加
OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr addObject:@4];
NSLog(@"%@", arr);
输出结果:
2016-04-01 01:28:47.111 OCTest[12297:2286198] (
1,
2,
3,
4
1,
2,
3,
4
)
swift:
var arr = [1, 2, 3]
arr.append(4);
arr.append(4);
print(arr)
输出结果: [1, 2, 3, 4]
var arr = [1, 2, 3]
arr += [4]
arr += [4]
print(arr)
输出结果: [1, 2, 3, 4]
var arr = [1, 2, 3]
arr += [4]
arr += [4]
arr += [5,6,7]
输出结果: [1, 2, 3, 4, 5, 6, 7]
var arr = [1, 2, 3]
arr += [4]
arr += [5,6,7]
arr += arr[0...3]
arr += [4]
arr += [5,6,7]
arr += arr[0...3]
print(arr)
输出结果: [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4]
插入
OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr insertObject:@4 atIndex:0];
NSLog(@"%@", arr);
输出结果:
2016-04-01 01:36:10.450 OCTest[12374:2339079] (
4,
1,
2,
3
4,
1,
2,
3
)
swift:
var arr = [1, 2, 3]
arr.insert(4, atIndex: 0);
arr.insert(4, atIndex: 0);
print(arr)
输出结果: [4, 1, 2, 3]
更新
OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
arr[0] = @8;
NSLog(@"%@", arr);
输出结果:
2016-04-01 01:37:28.482 OCTest[12397:2350177] (
8,
2,
3
8,
2,
3
)
swift:
var arr = [1, 2, 3]
arr[0] = 8
arr[0] = 8
print(arr)
输出结果: [8, 2, 3]
删除
OC:
NSMutableArray *arr1 =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr1 removeObject:@1];
NSLog(@"%@", arr1);
NSMutableArray *arr2 =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr2 removeLastObject];
NSLog(@"%@", arr2);
NSMutableArray *arr3 =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr3 removeAllObjects];
NSLog(@"%@", arr3);
输出结果:
2016-04-01 01:39:49.831 OCTest[12418:2370779] (
2,
3
)
2016-04-01 01:39:49.832 OCTest[12418:2370779] (
1,
2
)
2016-04-01 01:39:49.832 OCTest[12418:2370779] (
2,
3
)
2016-04-01 01:39:49.832 OCTest[12418:2370779] (
1,
2
)
2016-04-01 01:39:49.832 OCTest[12418:2370779] (
)
swift:
var arr1 = [1, 2, 3]
arr1.removeAtIndex(0)
print(arr1)
var arr2 = [1, 2, 3]
arr2.removeLast()
print(arr2)
arr1.removeAtIndex(0)
print(arr1)
var arr2 = [1, 2, 3]
arr2.removeLast()
print(arr2)
var arr3 = [1, 2, 3]
是否保持容量, 如果为true, 即便删除了容量依然存在, 容量是2的倍数
arr3.removeAll(keepCapacity: false)
print(arr3)
print(arr3.capacity)
输出结果:
[2, 3]
[1, 2]
[]
[1, 2]
[]
0
Range
OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr removeObjectsInRange:NSMakeRange(0, 2)];
NSLog(@"%@", arr);
输出结果:
2016-04-01 01:43:17.863 OCTest[12441:2396986] (
3
3
)
swift:
var arr = [1, 2, 3]
arr.removeRange(Range(start: 1, end: 2))
arr.removeRange(Range(start: 1, end: 2))
print(arr)
输出结果: [1, 3]
var arr = [1, 2, 3]
arr.removeRange(0...0)
arr.removeRange(0...0)
print(arr)
输出结果: [2, 3]
var range = 0...5
print(range)
输出结果: 0..<6