Swift 数组用于存储相同类型的值的顺序列表。Swift 要严格检查,它不允许错误地在数组中存放了错误的类型。
如果赋值创建数组到一个变量,它总是可变的,这意味着可以通过添加元素来改变它, 删除或更改其项目,但如果分配一个数组常量到则该数组,则数组是不可被改变的, 也就它的大小和内容不能被改变。
创建数组
可以使用下面的初始化程序语法来创建某种类型的空数组:
var someArray = [SomeType]()
var someArray = [SomeType](count: NumbeOfElements, repeatedValue: InitialValue)
var someInts = [Int](count: 3, repeatedValue: 0)
var someInts:[Int] = [10, 20, 30]
var someVar = someArray[index]
import Cocoavar someInts = [Int](count: 3, repeatedValue: 10)
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 Cocoavar someInts = [Int]()
someInts.append(20) someInts.append(30) someInts += [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 Cocoavar 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 Cocoavar someStrs = [String]()
someStrs.append("Apple") someStrs.append("Amazon") someStrs += ["Google"]
for item in someStrs { println(item) }
Apple Amazon Google
也可以使用 enumerate() 函数,如下面的例子所示,它返回索引及对应的值:
import Cocoavar 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 Cocoavar 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 Cocoavar 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 Cocoavar 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