話不多說
數組相關
空數組
var someInts = [Int]()
someInts.append(3) // someInts 現在包含一個 Int 值
someInts = [] // someInts 現在是空數組,但是仍然是 [Int] 類型的
//創建特定大小并且所有數據都被默認的構造方法
var threeDoubles = Array(repeating: 0.0, count: 3) // [0.0, 0.0, 0.0]
var threeDoubles2 = Array(repeating: 2.5, count: 3)
//(+)來組合兩個已存在的相同類型數組
var sixDoubles = threeDoubles + threeDoubles2 // [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
// 字面量構造數組
var shoppingList = ["Eggs", "Milk"]
// count
shoppingList.count
// 使用布爾屬性 isEmpty 作為一個縮寫形式去檢查 count 屬性是否為 0
shoppingList.isEmpty
// append(_:) 方法在數組后面添加新的數據項
shoppingList.append("Flour")
// (+=)直接將另一個相同類型數組中的數據添加到該數組后面:
shoppingList += ["Baking Powder"]
// 下標取值
var firstItem = shoppingList[0]
// 下標賦值
shoppingList[0] = "Six eggs"
//在某個指定索引值之前添加數據項:
shoppingList.insert("Maple Syrup", at: 0)
let mapleSyrup = shoppingList.remove(at: 0)
// 遍歷
for item in shoppingList {
print(item)
}
// enumerated() 返回一個由索引值和數據值組成的元組數組
for (index, value) in shoppingList.enumerated() {
print("Item \(String(index+1)):\(value)")
}
集合
集合類型的哈希值
一個類型為了存儲在集合中,必須提供一個方法來計算它的哈希值。一個哈希值是 Int 類型的,相等的對象哈希值必須相同,比如 a == b,因此必須 a.hashValue == b.hashValue。
// 空集合
var letters = Set<Character>()
letters.insert("a")
letters = []
// 字面量創建集合
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
favoriteGenres.count
favoriteGenres.isEmpty
// remove
if let removedGenre = favoriteGenres.remove("Rock") {
print("\(removedGenre) removed")
}else {
print("failed")
}
//contains
if favoriteGenres.contains("Funk") {
print("contains")
}
// 遍歷
for genre in favoriteGenres {
print("\(genre)")
}
// Set 類型沒有確定的順序,為了按照特定順序來遍歷一個集合中的值可以使用 sorted() 方法
for genre in favoriteGenres.sorted() {
print("\(genre)")
}
集合操作
集合操作
//使用“是否相等”運算符(==)來判斷兩個集合包含的值是否全部相同。
//使用 isSubset(of:) 方法來判斷一個集合中的所有值是否也被包含在另外一個集合中。
//使用 isSuperset(of:) 方法來判斷一個集合是否包含另一個集合中所有的值。
//使用 isStrictSubset(of:) 或者 isStrictSuperset(of:) 方法來判斷一個集合是否是另外一個集合的子集合或者父集合并且兩個集合并不相等。
//使用 isDisjoint(with:) 方法來判斷兩個集合是否不含有相同的值(是否沒有交集)。
字典
// 空字典
var namesOfIntegers = [Int: String]()
namesOfIntegers[16] = "sixteen"
namesOfIntegers = [:]
// 字面量創建字典
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
airports.count
airports.isEmpty
// 下標
airports["LHR"] = "London"
//updateValue(_:forKey:) 方法會返回對應值類型的可選類型。
//如果有值存在于更新前,則這個可選值包含了舊值,否則它將會是 nil :
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
print("The old value for DUB was \(oldValue).")
}
// 字典的下標訪問會返回對應值類型的可選類型
if let airportName = airports["DUB"] {
print("The name of the airport is \(airportName).")
}
// 從字典里移除一個鍵值對:使用下標語法通過將某個鍵的對應值賦值為 nil
airports["APL"] = "Apple Internation"
airports["APL"] = nil // APL 現在被移除了
// removeValue(forKey:)返回被移除的值或者在沒有對應值的情況下返回 nil:
if let removedValue = airports.removeValue(forKey: "DUB") {
print("The removed airport's name is \(removedValue).")
}
// 遍歷
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
for airportCode in airports.keys {
print("Airport code: \(airportCode)")
}
for airportName in airports.values {
print("Airport name: \(airportName)")
}