Swift 數組用於存儲相同類型的值的順序列表。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類型的初始值爲中括號內的數組
能夠使用下標語法從數組中檢索對應值,傳遞數組名後方括號內的索引對應的值,以下:app
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])" )
當上述代碼被編譯和執行時,它產生了如下結果:spa
Value of first element is 10 Value of second element is 10 Value of third element is 10
能夠使用 append() 方法或加法賦值運算符(+=)將新的項目添加到數組的末尾,在這裏首先建立一個空的數組,而後添加新的元素到數組中,以下圖所示:code
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
直接給已經存在的數組元素賦值就能夠修改數組元素中的值blog
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) }
當上述代碼被編譯和執行時,它產生了如下結果:element
Apple
Amazon
Google
也能夠使用 enumerate() 函數,以下面的例子所示,它返回索引及對應的值:it
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) 數組屬性,找出下面顯示出數組中元素的個數:
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