Swift array

来源:互联网 发布:python 入门教程pdf 编辑:程序博客网 时间:2024/06/16 22:18

在Objective-C中数组是常用的数据类型,在Swift中同样如此,在OC中有NSArray与NSMutableArray之分,但是在Swift中只有通过let和var来区分数组是否可变,Swift中的数组是类型安全的,所以在某个数据被存入到某个数组之前类型必须明确,假如我们创建了一个String类型的数组,那么该数组中就不能添加非String的数据类型,这是Swift与OC的一个很重要的区别。

数组的构造
我们以创建一个数组并存储Int类型的数组为例:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var array = [234, 5]  
  2. var array1: Array = [234, 5]  
  3. var array2: Array<Int> = [234, 5]  
  4. var array3: [Int] = [234, 5]  

数组的数量:数组有一个只读属性 count来获取数组中的数据项数量。
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. array.count  

检查数组是否为空可以用isEmpty来检查count是否为0
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. if array.isEmpty {  
  2.     println("array is Empty")  
  3. }else{  
  4.     println("array is not Empty")  
  5. }  

当要在数组后面添加新的数据项时可以使用append方法来添加:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. array .append(6)  

此时数组中就有5个值了,
当数组的数据类型为字符串时,也可以使用加法赋值运算符(+=)直接在数组的后面添加新的数据项;
加法运算符也可以直接添加拥有相同类型的数组:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. array += [7,8]  

获取数组中数据项的时候,可以用索引来获取值:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var intV = array[0]  

注:不管是OC还是Swift,数组的索引都是从0开始。

修改数组中的某一项时,也可以通过索引来改变:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. array[0] = 9  

此时数组就为:[9, 3, 4, 5, 6, 7, 8]
swift中也可以通过下标一次性改变多个数据值:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. array[1...3] = [10,11,12]  

此时数组值就是:[9, 10, 11, 12, 6, 7, 8]
在数组尾部添加新项时,不能使用下标来添加新项,此时数组越界,会引发一个运行期错误。

在某个具体索引值之前添加数据项,调用数组的insert(atIndex)来添加:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. array.insert(13, atIndex0)  

此时的数组值为:[13, 9, 10, 11, 12, 6, 7, 8]

同样的,移除数组的某一项时用removeAtIndex 方法;
移除最后一项的时候用removeLast 

数组遍历
普遍的我们使用for-in循环来遍历数组中的所有数据项
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. for i in array {  
  2.     println(i)  
  3. }  


swift提供一个enumerate函数来遍历数组,会同时返回数据项和索引值:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. for (index, value) in enumerate(array){  
  2.     println("index : \(index) value: \(value)")  
  3. }  


此时打印的值为:
index : 0 value: 13
index : 1 value: 9
index : 2 value: 10
index : 3 value: 11
index : 4 value: 12
index : 5 value: 6
index : 6 value: 7
index : 7 value: 8

如果我们需要创建一个数组,该数组有特定的大小并且所有的数据都是被默认的,此时swift提供了一个数组构造函数:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var newArray = [String](count: 4, repeatedValue"test")  

当然,swift具有类型推断功能,我们也可以如下定义:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var newArray2 = Array(count: 3, repeatedValue"today")  

我们知道,两种数据项类型相同的数组我们可以通过(+)来组合到一起形成一个新的数组:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var newArray3 = newArray + newArray2  



在苹果的开发文档中对Array还提供了其他的操作算法:

1、Sort函数:

对数组进行排序,根据指定的排序规则,看下面的代码:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var array = [234, 5]  
  2.   
  3. array.sort{$0 < $1}  
  4. println(array)  

此时打印出:[2, 3, 4, 5]
其实sort大括号里面是一个闭包,这个以后再学习。
如果倒叙排列,代码如下:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. array.sort{$0 > $1}  
  2. println(array)  

输出为:[5, 4, 3, 2]

2、reverse函数
按照数组的index倒叙排列返回,比如:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. let newA = array.reverse()  
  2. println(newA)  

此时的输出应该为:[2, 3, 4, 5]
可以看出数组倒叙排列了。

3、filter
根据某些条件来筛选数组值:比如:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. let newB = array.filter{$0 % 2 == 0}  
  2. println(newB)  

输出为:[2, 4]

4、map
对当前数组运用闭包内的规则然后返回一个新的数组:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var newArray = array.map{$00 * 3}  
  2. println(newArray)  

闭包内的规则是每个数字乘以3 所以我们的输出为:[6, 9, 12, 15]

5、reduce
官方解释为:通过闭包内对每个元素进行操作然后返回一个单独的值
比如:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. let addRes = array.reduce(2){$0 + $1}  
  2. println(addRes)  

reduce(2)括号中的2表示初始值,闭包内为操作

我们得到的返回值为:16

PS:Swift还提供了一个Slice类,官方描述如下:

The slice class represents a one-dimensional subset of an array, specified by three parameters: start offset, size, and stride. The start offset is the index of the first element of the array that is part of the subset. The size is the total number of elements in the subset. Stride is the distance between each successive array element to include in the subset.

For example, with an array of size 10, and a slice with offset 1, size 3 and stride 2, the subset consists of array elements 1, 3, and 5.

简单的说就是Slice是Array的一个子类,包含三个部分:start offset, size, stride。

看个例子:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var slice : Slice = array[1...3]  
  2. array = Array(slice)  
  3. slice = Slice(array)  
  4. println(slice)  
0 0
原创粉丝点击