swift學習筆記3 集合類型

注:英文部分來自官方文檔

It is good practice to create immutable collections in all cases where the collection does not need to change. Doing so makes it easier for you to reason about your code and enables the Swift compiler to optimize the performance of the collections you create.

如果集合不需要改變的情況下,最好是創建不可變的集合。這樣做可以更容易地推導你的代碼,並且swift編譯器也能更容易地優化你創建的集合。

數組

  • 把兩個數組相加來創建新數組
    你可以把兩個已存在的擁有兼容類型的數組相加來創建一個新的數組。
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles 是 [Double]類型, 值是 [2.5, 2.5, 2.5]

var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles 也是 [Double]類型, 值是[0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

You can add a new item to the end of an array by calling the array’s append(_:) method:

可以通過調用append(_:)方法把一個新的item添加到數組末尾

var shoppingList = ["Eggs", "Milk"]
shoppingList.append("Flour")
// shoppingList 現在包含 3 items, 

Alternatively, append an array of one or more compatible items with the addition assignment operator (+=):

另外一種方法是使用操作符+=來添加一個擁有兼容項的數組。

shoppingList += ["Baking Powder"]
// shoppingList 現在包含 4 items
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList 現在包含 7 items

You can use subscript syntax to change an existing value at a given index:

可以使用下標語法來修改一個在給定索引的值。

shoppingList[0] = "Six eggs"
// 數組的第一個值現在是 "Six eggs" 而不是"Eggs"

When you use subscript syntax, the index you specify needs to be valid. For example, writing shoppingList[shoppingList.count] = “Salt” to try to append an item to the end of the array results in a runtime error.

當你使用下標語法時,你指定的下標必須是有效的。舉個例子,
shoppingList[shoppingList.count] = "Salt"
上面的代碼中,下標越界了,會報一個運行時錯誤。

You can also use subscript syntax to change a range of values at once, even if the replacement set of values has a different length than the range you are replacing. The following example replaces “Chocolate Spread”, “Cheese”, and “Butter” with “Bananas” and “Apples”:

你還可以使用下標語法一次性地修改一個範圍內的值,即使用來替換的值的長度與被替換的範圍的長度不一致。下面的例子用”Bananas” 和 “Apples”替換 “Chocolate Spread”, “Cheese”, 和 “Butter”

shoppingList[4...6] = ["Bananas", "Apples"]
// shoppingList 現在包含 6 items

If you need the integer index of each item as well as its value, use the enumerated() method to iterate over the array instead. For each item in the array, the enumerated() method returns a tuple composed of an integer and the item. The integers start at zero and count up by one for each item; if you enumerate over a whole array, these integers match the items’ indices. You can decompose the tuple into temporary constants or variables as part of the iteration:

如果你需要每個元素的值與索引的值,可以使用enumerated()去遍歷數組。對於數組中的每一項,enumerated()方法都會返回一個包含一個整數和該元素的tuple。整數值從0開始,每次加1。如果你遍歷整個數組,這些整數會和元素的索引向吻合。你可以把tuple解析爲遍歷的一部分的臨時常量或變量

for (index, value) in shoppingList.enumerated() {
    print("Item \(index + 1): \(value)")
}
// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Bananas

Set類型

  • set的基礎操作
    下圖顯示了兩個set a 和 b的各種操作的結果

Paste_Image.png

Use the intersection(_:) method to create a new set with only the values common to both sets.
Use the symmetricDifference(_:) method to create a new set with values in either set, but not both.
Use the union(_:) method to create a new set with all of the values in both sets.
Use the subtracting(_:) method to create a new set with values not in the specified set.

intersection(_:)方法用於取兩個set都擁有的值。
symmetricDifference(_:) 方法用於取兩個set中不共同擁有的值。
union(_:) 用於取兩個set中的所有的值。
subtracting(_:)方法用於取不在某個特定set中的值。

let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]

oddDigits.union(evenDigits).sorted()
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersection(evenDigits).sorted()
// []
oddDigits.subtracting(singleDigitPrimeNumbers).sorted()
// [1, 9]
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()
// [1, 2, 9]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章