Swift 数组用于存储相同类型的值的顺序列表。Swift 要严格检查,不允许不同类型的值在同一个数组中
声明一个数组
var someArray = [SomeType]() var someArray = [SomeType](count: NumbeOfElements, repeatedValue: InitialValue) var someInts = [Int](count: 3, repeatedValue: 0)//声明一个三个容量的数组,初始值为0 var someInts:[Int] = [10, 20, 30]//声明一个int类型的初始值为中括号内的数组
可以使用下标语法从数组中检索对应值,传递数组名后方括号内的索引对应的值,如下:
var someVar = someArray[index]
在这里,指数从0开始,这意味着可以使用索引0来访问第一个元素,第二元素可以通过使用索引1进行访问,其它类似。让我们来看看下面创建,初始化和访问数组的例子:
import Cocoa var someInts = [Int](count: 3, repeatedValue: 10)//创建一个3个空间的可变数组,初始值都是10,数组类型为int类型 var someVar = someInts[0] println( "Value of first element is \(someVar)" ) println( "Value of second element is \(someInts[1])" ) println( "Value of third element is \(someInts[2])" )
当上述代码被编译和执行时,它产生了以下结果:
Value of first element is 10 Value of second element is 10 Value of third element is 10
修改数组
可以使用 append() 方法或加法赋值运算符(+=)将新的项目添加到数组的末尾,在这里首先创建一个空的数组,然后添加新的元素到数组中,如下图所示:
import Cocoa var someInts = [Int]()//初始化一int类型的空数组 someInts.append(20)//添加20 someInts.append(30)//添加30 someInts += [40]//添加40 var someVar = someInts[0] println( "Value of first element is \(someVar)" ) println( "Value of second element is \(someInts[1])" ) println( "Value of third element is \(someInts[2])" )
Value of first element is 20 Value of second element is 30 Value of third element is 40
直接给已经存在的数组元素赋值就可以修改数组元素中的值
import Cocoa var someInts = [Int]() someInts.append(20) someInts.append(30) someInts += [40] // Modify last element someInts[2] = 50 var someVar = someInts[0] println( "Value of first element is \(someVar)" ) println( "Value of second element is \(someInts[1])" ) println( "Value of third element is \(someInts[2])" )
Value of first element is 20 Value of second element is 30 Value of third element is 50
可以使用 for-in 循环迭代级数,在下面的例子是数组的整个集值,如下图所示:
import Cocoa var someStrs = [String]() someStrs.append("Apple") someStrs.append("Amazon") someStrs += ["Google"] for item in someStrs { println(item) }
当上述代码被编译和执行时,它产生了以下结果:
Apple
Amazon
Google
也可以使用 enumerate() 函数,如下面的例子所示,它返回索引及对应的值:
import Cocoa var someStrs = [String]() someStrs.append("Apple") someStrs.append("Amazon") someStrs += ["Google"] for (index, item) in enumerate(someStrs) { println("Value at index = \(index) is \(item)") }
当上述代码被编译和执行时,它产生了以下结果:
Value at index = 0 is Apple
Value at index = 1 is Amazon
Value at index = 2 is Google
两个数组相加
使用加法运算符(+),以添加的相同类型的数组,这将产生新的数组是来自两个数组值相加组合后的数组,如下:
import Cocoa var intsA = [Int](count:2, repeatedValue: 2) var intsB = [Int](count:3, repeatedValue: 1) var intsC = intsA + intsB for item in intsC { println(item) }
当上述代码被编译和执行时,它产生了以下结果:
2 2 1 1 1
count 属性
可以使用只读计算 (count) 数组属性,找出下面显示出数组中元素的个数:
import Cocoa var intsA = [Int](count:2, repeatedValue: 2) var intsB = [Int](count:3, repeatedValue: 1) var intsC = intsA + intsB println("Total items in intsA = \(intsA.count)") println("Total items in intsB = \(intsB.count)") println("Total items in intsC = \(intsC.count)")
当上述代码被编译和执行时,它产生了以下结果:
Total items in intsA = 2 Total items in intsB = 3 Total items in intsC = 5
空属性
使用只读数组的空属性(isEmpty)找出一个数组是否为空,如下图所示:
import Cocoa var intsA = [Int](count:2, repeatedValue: 2) var intsB = [Int](count:3, repeatedValue: 1) var intsC = [Int]() println("intsA.isEmpty = \(intsA.isEmpty)") println("intsB.isEmpty = \(intsB.isEmpty)") println("intsC.isEmpty = \(intsC.isEmpty)")
当上述代码被编译和执行时,它产生了以下结果:
intsA.isEmpty = false intsB.isEmpty = false intsC.isEmpty = true