近日在學習swift,對一些基本數據的用法做了學習總結,本篇主要對swift中的Dictionary的用法進行了總結 :
//字典 //創建一個空字典
var namesOfIntegers = [Int: String]() //鍵是Int型 值是String型 // namesOfIntegers 是一個空的 [Int: String] 字典
namesOfIntegers[16] = "sixteen"// namesOfIntegers 現在包含一個鍵值對 //如果上下文已經提供了類型信息,我們可以使用空字典字面量來創建一個空字典,記作[:](中括號中放一個冒號)
namesOfIntegers = [:] // namesOfIntegers 又成為了一個 [Int: String] 類型的空字典 //用字典的字面量創建字典 // [key 1: value 1, key 2: value 2, key 3: value 3]
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]//airports這個字典字面量的任務是構造擁有兩個初始數據項的字典,字典被聲明為一種[String: String]類型,這意味着這個字典的鍵和值都是String類型 //和數組一樣,我們在用字典字面量構造字典時,如果它的鍵和值都有各自一致的類型,那么就不必寫出字典的類型。 airports字典也可以用這種簡短方式定義: //var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"] //因為這個語句中所有的鍵和值都各自擁有相同的數據類型,Swift 可以推斷出Dictionary<String, String>是airports字典的正確類型。 //訪問和修改字典 //獲取字典的鍵值對數量
print("The dictionary of airports contains \(airports.count) items.")//打印 The dictionary of airports contains 2 items. //判斷字典是否為空 count是否為0
if airports.isEmpty{ print("The airports dictionary is empty.") }else{ print("The airpotes dictionary is not empty.") }//打印 The airpotes dictionary is not empty. //使用下標語法添加新的數據項
airports["LHR"] = "London"// airports 字典現在有三個數據項了 //使用下標語法改變特定鍵對應的值
airports["LHR"] = "London Heathrow" //LHR對應的值被改成了“London Heathrow” //作為另一種下標方法,字典的updateValue(_:forKey:)方法可以設置或者更新特定鍵對應的值。就像上面所示的下標示例,updateValue(_:forKey:)方法在這個鍵不存在對應值的時候會設置新值或者在存在時更新已存在的值。和上面的下標方法不同的,updateValue(_:forKey:)這個方法返回更新值之前的原值。這樣使得我們可以檢查更新是否成功, updateValue(_:forKey:)方法會返回對應值的類型的可選值。 //如果在更新前有值,那返回的這個可選值包含舊值否則是nil
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") { //oldValue 返回的就是更新前的舊值,如果更新前鍵DUB沒有對應的值,那此時的返回值oldValue就是nil
print("The old value for DUB was \(oldValue).")// 輸出 "The old value for DUB was Dublin."
} //用下標訪問鍵對應的值,因為有可能請求的鍵沒有對應的值存在,字典的下標訪問會返回對應值的類型的可選值。如果這個字典包含請求鍵所對應的值,下標會返回一個包含這個存在值的可選值,否則將返回nil:
if let airportNameDUB = airports["DUB"] {//airportNameDUB是請求訪問鍵DUB對應的可選值
print("The name of the airport is \(airportNameDUB).") } else { print("That airport is not in the airports dictionary.") } // 打印 "The name of the airport is Dublin Airport." //可以使用下標語法來通過給某個鍵的對應值 賦值 為 “nil” 來從字典里 移除一個鍵值對
airports["APL"] = "Apple Internation"
// "Apple Internation" 不是真的 APL 機場, 刪除它
airports["APL"] = nil // APL 現在被從字典里移除了 //removeValue(forKey:)方法也可以用來在字典中移除鍵值對。這個方法在鍵值對存在的情況下會移除該鍵值對並且返回被移除的值或者在沒有值的情況下返回nil
if let removedValue = airports.removeValue(forKey: "DUB") { print("The removed airport's name is \(removedValue).") } else { print("The airports dictionary does not contain a value for DUB.") } //打印 prints "The removed airport's name is Dublin Airport." //字典的遍歷,使用for - in 循環來遍歷字典中的鍵值對,每個字典中的數據項都以(key,value)元組的形式返回,可以使用臨時常量或者變量來分解這些元組
for (airportKey,airportName) in airports { print("字典遍歷的結果:\(airportKey):\(airportName)") //打印 //字典遍歷的結果:YYZ:Toronto Pearson //字典遍歷的結果:LHR:London Heathrow
} //可以單獨遍歷出字典里的所有keys 或者是 values值
for airportKey in airports.keys{//獲取字典中所有的鍵
print("airport key:\(airportKey)") //打印 //airport key:YYZ //airport key:LHR
} for airportName in airports.values{//獲取字典中的所有值
print("airport name:\(airportName)") //打印 //airport name:Toronto Pearson //airport name:London Heathrow
} //當只是需要使用某個字典的鍵集合或者值集合來作為某個接收Array實例的 API 的參數,可以直接使用keys或者values屬性構造一個新數組
let airportKeyArray = [String](airports.keys)//獲取字典里的鍵構造成新數組“airportKeyArray”
print("airportAllKeys:\(airportKeyArray)")//打印airportAllKeys:["YYZ", "LHR"]
let airportNameArray = [String](airports.values)//獲取字典里的所有的值構造成新的數組“airportNameArray”
print("airportAllValues:\(airportNameArray)")//打印airportAllValues:["Toronto Pearson", "London Heathrow"] //Swift 的字典類型是無序集合類型。為了以特定的順序遍歷字典的鍵或值,可以對字典的keys或values屬性使用sorted()方法。