Swift6 - 集合類型相關(guān)

話不多說

數(shù)組相關(guān)

空數(shù)組
var someInts = [Int]()
someInts.append(3) // someInts 現(xiàn)在包含一個 Int 值
someInts = [] // someInts 現(xiàn)在是空數(shù)組,但是仍然是 [Int] 類型的

//創(chuàng)建特定大小并且所有數(shù)據(jù)都被默認的構(gòu)造方法
var threeDoubles = Array(repeating: 0.0, count: 3) // [0.0, 0.0,  0.0]
var threeDoubles2 = Array(repeating: 2.5, count: 3)

//(+)來組合兩個已存在的相同類型數(shù)組
var sixDoubles = threeDoubles + threeDoubles2 // [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

// 字面量構(gòu)造數(shù)組
var shoppingList = ["Eggs", "Milk"]
// count
shoppingList.count
// 使用布爾屬性 isEmpty 作為一個縮寫形式去檢查 count 屬性是否為 0
shoppingList.isEmpty
//  append(_:) 方法在數(shù)組后面添加新的數(shù)據(jù)項
shoppingList.append("Flour")
// (+=)直接將另一個相同類型數(shù)組中的數(shù)據(jù)添加到該數(shù)組后面:
shoppingList += ["Baking Powder"]
// 下標取值
var firstItem = shoppingList[0]
// 下標賦值
shoppingList[0] = "Six eggs"

//在某個指定索引值之前添加數(shù)據(jù)項:
shoppingList.insert("Maple Syrup", at: 0)
let mapleSyrup = shoppingList.remove(at: 0)
// 遍歷
for item in shoppingList {
    print(item)
}
// enumerated() 返回一個由索引值和數(shù)據(jù)值組成的元組數(shù)組
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 = []
// 字面量創(chuàng)建集合
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 = [:]
// 字面量創(chuàng)建字典
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
airports.count
airports.isEmpty
// 下標
airports["LHR"] = "London"
//updateValue(_:forKey:) 方法會返回對應(yīng)值類型的可選類型。
//如果有值存在于更新前,則這個可選值包含了舊值,否則它將會是 nil :
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
    print("The old value for DUB was \(oldValue).")
}
// 字典的下標訪問會返回對應(yīng)值類型的可選類型
if let airportName = airports["DUB"] {
    print("The name of the airport is \(airportName).")
}
// 從字典里移除一個鍵值對:使用下標語法通過將某個鍵的對應(yīng)值賦值為 nil
airports["APL"] = "Apple Internation"
airports["APL"] = nil // APL 現(xiàn)在被移除了

// removeValue(forKey:)返回被移除的值或者在沒有對應(yīng)值的情況下返回 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)")
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容