Swift--数组和字典(一)
數組是一個存儲多個相同類型的值的有序列表。相同的值,可以在不同的位置出現在一個數組中的多個次。
Swift數組是具體的。他不同于Objective-C的的NSArray和NSMutableArray里的類,它可以存儲任何類型的對象,不提供有關它們返回的對象的性質的任何信息。在斯Swift,一個特定的數組可以存儲的值類型總是明確的,無論是通過顯式類型批注,或通過類型推斷,而不一定是類類型。如果創建詮釋值的數組,例如,你不能插入比Int值以外的任何值到該數組。Swift數組是類型安全的,并且總是清楚它們可能含有什么。
?
數組語法
數組的類型是Array<SomeType>,其中個SomeType是該數組允許存儲的類型。你也可以寫簡寫形式數組的類型為個SomeType[]。雖然兩種形式在功能上是相同的,簡寫形式是首選,而指的是一個數組類型時使用本指南。
?
?
您可以初始化數組與數組文本,這是一種速記的方式來寫一個或多個值作為數組集合。數組常寫為值列表,以逗號分隔,由一對方括號包圍:
[value1,value2, value3]
實例如下:
.????? ??var shoppingList: String[] = ["Eggs", "Milk"]
.????? ??//shoppingList has been initialized with two initial items
該shoppingList變量聲明為“字符串值的數組”,寫為String[]。因為這個特殊的數組指定字符串的值類型,它僅允許存儲字符串值。在這里,shoppingList數組與兩個字符串值(“Eggs”和“Milk”)進行初始化,寫入數組字面之內。
?
由于Swift的類型推斷,你可以不用寫入數組的類型,如果你初始化它與同類型的數組字面含值。 shoppingList的初始化可以寫一個較短的形式:
var shoppingList = ["Eggs","Milk"]
由于數組中都是字符串類型,所以可以如上這么寫.
?
?
訪問和修改數組
我們可以通過數組的方法和屬性,也可以通過語法來實現.
我們可以通過只讀的count屬性來獲取count值.
.????? println("The shopping listcontains \(shoppingList.count) items.")
.????? ?// prints "The shopping list contains2 items."
通過數組的isEmpty屬性來檢測是否為空
?
?
?
if shoppingList.isEmpty {
??? println("The shopping list is empty.")
} else{
println("The shoppinglist is not empty.")
?
}?// prints "Theshopping list is not empty."
?
你可以通過append在數組最后增加一個元素
?shoppingList.append("Flour")
?//shoppingList now contains 3 items, and someone is making pancakes
你也可以通過+=來實現增加一個元素到數組最后
.????? shoppingList += "Baking Powder"
.????? ?// shoppingList now contains 4 items
?
.????? shoppingList += ["ChocolateSpread", "Cheese", "Butter"]
??//shoppingList now contains 7 items
通過使用語法標從數組中檢索值,傳遞你要檢索中的值的索引
立即數組的名稱之后方括號:
.????? var firstItem = shoppingList[0]
.????? ??//firstItem is equal to "Eggs"
請注意,數組中的第一項為0,而不是1。Swift數組是始終為零索引。
shoppingList[0] = "Six eggs"
// the first item in the list is now equal to "Sixeggs" rather than "Eggs"
你也可以使用下標語法來改變一個范圍值一次,即使更換一組值有
不同長度的比你要更換的范圍內。下面的示例將“Chocolate Spread”,“Cheese”,
和“Butter”與“Bananas”和“Apples”:
shoppingList[4...6]= ["Bananas", "Apples"]
//shoppingList now contains 6 items
?
插入一個項目到數組中指定索引處,調用數組的insert(atIndex:)方法:
shoppingList.insert("MapleSyrup", atIndex: 0)
//shoppingList now contains 7 items
//"Maple Syrup" is now the first item in the list
?
同樣,你可以使用removeAtIndex方法刪除數組中一個項目。此方法刪除了該項目的指定索引,并返回被刪除的項目(盡管你可以忽略返回值,如果你不需要它):
letmapleSyrup = shoppingList.removeAtIndex(0)
//the item that was at index 0 has just been removed
//shoppingList now contains 6 items, and no Maple Syrup
//the mapleSyrup constant is now equal to the removed "Maple Syrup"string
如果你想從數組中刪除最后一個項目,使用removeLast方法,而不是removeAtIndex
方法以避免需要查詢數組的Count屬性。像removeAtIndex方法,removeLast
返回已刪除項目:
letapples = shoppingList.removeLast()
//the last item in the array has just been removed
//shoppingList now contains 5 items, and no cheese
//the apples constant is now equal to the removed "Apples" string
?
迭代數組
你可以使用for-in來完成數組迭代
foritem in shoppingList {??
?println(item)
}
//Six eggs
//Milk
//Flour
//Baking Powder
//Bananas
?
如果你需要的每個項目,以及該整數索引的值,使用全局枚舉函數來遍歷
數組代替。該枚舉函數返回一個元組每個項目的指標組成的數組中和
值該項目。你可以分解成元組臨時常量或變量作為迭代的一部分:
for(index, value) in enumerate(shoppingList) {
??? println("Item \(index + 1):\(value)")
}
//Item 1: Six eggs
//Item 2: Milk
//Item 3: Flour
//Item 4: Baking Powder
//Item 5: Bananas
?
數組的初始化
你可以給一個數組確定的類型:
varsomeInts = Int[]()
println("someIntsis of type Int[] with \(someInts.count) items.")
//prints "someInts is of type Int[] with 0 items."
另外,如果環境中已經提供的類型信息,如函數參數或者一個已經鍵入
變量或常量,你可以創建一個空數組,空數組字面,這是寫為[](空
一對方括號):
someInts.append(3)
//someInts now contains 1 value of type Int
someInts= []
//someInts is now an empty array, but is still of type Int[]
Swift的數組類型還提供了一個初始化用于創建具有一定規模與所有其值設置為提供默認值的數組。你通過這個初始化器被添加到新的數組(名為count)的項目數和
適當的類型(稱為repeatedValue)的默認值:
varthreeDoubles = Double[](count: 3, repeatedValue: 0.0)
//threeDoubles is of type Double[], and equals [0.0, 0.0, 0.0]
由于類型推斷,就不需要指定使用該初始值,當要被存儲在數組中的類型,
因為它可以從默認值來推斷:
varanotherThreeDoubles = Array(count: 3, repeatedValue: 2.5)
//anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5]
最后,你可以通過與另外加在一起兼容類型的兩個現有陣列創建一個新的數組
運算符(+)。新數組的類型是從兩個數組你加在一起的類型推斷:
varsixDoubles = threeDoubles + anotherThreeDoubles
//sixDoubles is inferred as Double[], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
總結
以上是生活随笔為你收集整理的Swift--数组和字典(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为公司已起诉小米专利侵权 国家知识产权
- 下一篇: Swift--数组和字典(二)