一. 数组定义(比较 OC 与 Swift 的数组)
1.1. 有值数组
//oc 有值数组
NSArray *arr0 = @[@1, @2, @3];
NSArray *arr1 = [NSArray arrayWithObjects:@1, @2, @3, nil];
NSLog(@"%@", arr1);
/*输出结果:
2016-01-06 01:04:49.180 test[11687:2096671] (
1,
2,
3
)*/
//swift有值数组
var arr0 = [1, 2, 3]var arr1: Array = [1, 2, 3]var arr2: Array<Int> = [1, 2, 3]
var arr3: [Int] = [1, 2, 3]
print(arr3)
//输出结果: [1, 2, 3]
1.2. 空数组
//oc空数组
NSArray *arr = @[];
NSArray *arr1 = [NSArray array];
NSLog(@"%@", arr1);
/*输出结果:
2016-01-06 01:06:04.132 test[11703:2106199] (
)*/
//swift空数组
var arr5 = []var arr6 = [Int]()var arr7 = Array<Int>()
print(arr7)
//输出结果: []
1.3. 可变与不可变数组
// oc
NSArray //不可变数组:
NSMutableArray//可变数组:
// swift
//可变数组:
var arr8 = []
//不可变数组:
let arr9 = []
1.4. 元素类型
//OC:
NSArray *arr = @[@1, @"cdh", @130];
NSLog(@"%@", arr);
/*输出结果:
2016-01-06 01:13:07.987 test[11891:2163044] (
1,
cdh,
130
)*/
//swift: (常用写法)
var arr = [1, "CDH", 130] // 编译器自行推导出 arr : [NSObject]
print(arr)
//输出结果: [1, CDH, 130]
//如果想明确表示数组中存放的是不同类型的数据
//可以使用Any 或者 NSObject 关键字, 表示数组中可以存放不同类型的数据
var arr10:Array<Any> = [1, "CDH", 130] // 存放的字符串是带有双引号的
print(arr10)
//输出结果: [1, "CDH", 130]
// 注意 打印出来的字符串是带有双引号的
var arr12 : [NSObject] = [2, "cdh", 18]
print(arr12)
//输出结果:[2, cdh, 18]
//指定数组当中只能存放Int型,如果存放其它类型会直接报错
var arr14:Array<Int> = [1, 10, 130]
print(arr14)
//输出结果: [1, 10, 130]
var arr15:[Int] = [1,2,3]
print(arr15)
//输出结果: [1, 2, 3]
二. 数组操作
2.1. 获取长度
//OC:
NSArray *arr = @[@1, @2, @3];
NSLog(@"%tu", arr.count);
//输出结果: 2016-01-06 01:19:54.874 test[12086:2209180] 3
//swift:
var arr = [1, 2, 3]
print(arr.count)
//输出结果:3
2.2. 判断是否为空
//OC:
NSArray *arr = @[];
NSLog(@"result = %d", arr.count != 0);
//输出结果: 2016-01-06 01:25:14.252 OCTest[12249:2255010] result = 0
var arr1 = [1, 2, 3]
print(arr1.isEmpty)
//输出结果: false
-
注意: 只有该数组有指定的存储类型,或者数组中有元素才能使用 isEmpty 这个方法, 不然会报错, 比如:
var arr = [] // 这是个空的数组, 而且有没有指定类型, 只表示为 Array 类型
print(arr.isEmpty) // 是会报错的
2.3. 检索
//OC:
NSArray *arr = @[@1, @2, @3];
NSLog(@"%@", arr[0]);
//输出结果: 2016-01-06 01:26:46.816 OCTest[12275:2269853] 1
//swift:
var arr2 = [1, 2, 3]
print(arr2[0])
//输出结果: 1
2.4. 追加
//OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr addObject:@4];
NSLog(@"%@", arr);
/*输出结果:
2016-01-06 01:28:47.111 OCTest[12297:2286198] (
1,
2,
3,
4
)*/
//swift:
var arr3 = [1, 2, 3]
arr3.append(4);
print(arr3)
//输出结果: [1, 2, 3, 4]
var arr4 = [1, 2, 3]
arr4 += [4]
print(arr4)
//输出结果: [1, 2, 3, 4]
var arr5 = [1, 2, 3]
arr5 += [4]
arr5 += [5,6,7]
//输出结果: [1, 2, 3, 4, 5, 6, 7]
var arr6 = [1, 2, 3]
arr6 += [4]
arr6 += [5,6,7]
arr6 += arr6[0...3]
print(arr6)
//输出结果: [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4]
2.5. 插入
//OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr insertObject:@4 atIndex:0];
NSLog(@"%@", arr);
/*输出结果:
2016-01-06 01:36:10.450 OCTest[12374:2339079] (
4,
1,
2,
3
)*/
//swift:
var arr7 = [1, 2, 3]
arr7.insert(4, atIndex: 0);
print(arr7)
//输出结果: [4, 1, 2, 3]
2.6. 更新
//OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
arr[0] = @8;
NSLog(@"%@", arr);
/*输出结果:
2016-01-06 01:37:28.482 OCTest[12397:2350177] (
8,
2,
3
)*/
//swift:
var arr8 = [1, 2, 3]
arr8[0] = 8
print(arr8)
//输出结果: [8, 2, 3]
2.7. 删除
//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-01-06 01:39:49.831 OCTest[12418:2370779] (
2,
3
)
2016-01-06 01:39:49.832 OCTest[12418:2370779] (
1,
2
)
2016-01-06 01:39:49.832 OCTest[12418:2370779] (
)*/
//swift:
var arr9 = [1, 2, 3]
arr9.removeAtIndex(0)
print(arr9)
// 输出结果:[2, 3]
var arr10 = [1, 2, 3]
arr10.removeLast()
print(arr10)
// 输出结果:[1, 2]
var arr11 = [1, 2, 3]
//是否保持容量, 如果为true, 即便删除了容量依然存在, 容量是2的倍数
arr11.removeAll(keepCapacity: false)
print(arr11)
print(arr11.capacity)
//输出结果:
//[]
//0
2.8. Range
//OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
[arr removeObjectsInRange:NSMakeRange(0, 2)];
NSLog(@"%@", arr);
/*输出结果:
2016-01-06 01:43:17.863 OCTest[12441:2396986] (
3
)*/
//swift:
var arr12 = [1, 2, 3]
arr12.removeRange(Range(start: 1, end: 2))// 这里3.0 之后就不能用这个 Range(start: , end: )方法了 而是推荐使用 range = ..< 这样的取范围
print(arr12)
//输出结果: [1, 3]
var arr13 = [1, 2, 3]
arr13.removeRange(0...0)
print(arr13)
//输出结果: [2, 3]
var range14 = 0...5
print(range14)
//输出结果: 0..<6
三. 数组的其它操作
3.1. 数组的批量操作
//OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
NSRange range = NSMakeRange(0, 2);
[arr replaceObjectsInRange:range
withObjectsFromArray:@[@99, @88, @77, @66]];
NSLog(@"%@", arr);
/*输出结果:
2016-01-06 14:56:45.493 OCTest[3599:667081] (
99,
88,
77,
66,
3
)*/
//swift:
var arr = [1, 2, 3]
arr.replaceRange(1..<2, with: [99, 88, 77, 66])
print(arr)
//输出结果: [1, 99, 88, 77, 66, 3]
var arr1 = [1, 2, 3]
arr1.replaceRange(Range(start: 0, end: 2), with: [99, 88, 77, 66])
print(arr1)
//输出结果: [99, 88, 77, 66, 3]
3.2. 遍历
//OC:
NSArray *arr = @[@1, @2, @3];
for (int i = 0; i < arr.count; i++) {
NSLog(@"%@", arr[i]);
}
/*输出结果:
2016-01-06 15:00:10.702 OCTest[3635:694273] 1
2016-01-06 15:00:10.703 OCTest[3635:694273] 2
2016-01-06 15:00:10.703 OCTest[3635:694273] 3
*/
NSArray *arr = @[@1, @2, @3];
for (NSNumber *number in arr) {
NSLog(@"%@", number);
}
/*输出结果:
2016-01-06 15:03:43.192 OCTest[3684:723081] 1
2016-01-06 15:03:43.193 OCTest[3684:723081] 2
2016-01-06 15:03:43.194 OCTest[3684:723081] 3
*/
//swift:
var arr2 = [1, 2, 3]
// 这种写法将在 swift3.0 被移除, 不能被使用
for var i = 0 ; i < arr2.count ; i++{
print(arr2[i])
}
// 推荐的使用一下三种方法
for i in 0 ..< arr2.count{
print(arr2[i])
}
for number in arr2{
print(number)
}
//指定起始范围
for number in arr2[0..<3]{
print(number)
}
//输出结果都是:
//1
//2
//3
网友评论