[toc]
數(shù)組
初始化一個(gè)數(shù)組
var someInts = [Int]()
someInts.append(2)
someInts = []
// 創(chuàng)建帶有默認(rèn)值的數(shù)組
var threeDoubles = Array(repeating: 0.0, count: 3)
// 兩個(gè)數(shù)組相加創(chuàng)建新數(shù)組
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
var sixDoubles = threeDoubles + anotherThreeDoubles
// 數(shù)組字面量創(chuàng)建數(shù)組
var shoppingList: [String] = ["Eggs", "Milk"]
增加數(shù)組元素
shoppingList.append("Flour")
shoppingList += ["Baking Powder"]
shoppingList.insert("Maple Syrup", at: 4)
讀取數(shù)組元素
var firstItem = shoppingList[0]
修改數(shù)組元素
shoppingList[0] = "Six eggs"
shoppingList[1...3] = ["Bananas", "Apple", "orange"]
print(shoppingList)
刪除數(shù)組元素
shoppingList.remove(at: 0)
shoppingList.removeFirst()
shoppingList.removeLast()
數(shù)組遍歷
for item in shoppingList {
print(item)
}
for (index, value) in shoppingList.enumerated() {
print("index: \(index), value: \(value)")
}
集合
集合類型的哈希值
相等的對(duì)象哈希值必須相同 比如 a==b 必須a.hasValue == b.hasValue
Swift 的所有基本類型(比如 String,Int,Double 和 Bool)默認(rèn)都是可哈纤跻耍化的
可以作為集合的值的類型或者字典的鍵的類型鲸阔。沒(méi)有關(guān)聯(lián)值的枚舉成員值默認(rèn)也是可哈戏杼耍化的
因?yàn)?Hashable 協(xié)議符合 Equatable 協(xié)議晦譬,所以遵循該協(xié)議的類型也必須提供一個(gè)“是否相等”運(yùn)算符(==)的實(shí)現(xiàn)叭首。這個(gè) Equatable 協(xié)議要求任何符合 == 實(shí)現(xiàn)的實(shí)例間都是一種相等的關(guān)系习勤。也就是說(shuō),對(duì)于 a,b,c 三個(gè)值來(lái)說(shuō)焙格,== 的實(shí)現(xiàn)必須滿足下面三種情況:
a == a(自反性)
a == b 意味著 b == a(對(duì)稱性)
a == b && b == c 意味著 a == c(傳遞性)
創(chuàng)建和構(gòu)造一個(gè)空集合
var letters = Set<Character>()
letters.insert("a")
letters = []
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
var favoriteGenres1: Set = ["Rock", "Classical", "Hip hop"]
訪問(wèn)集合
print("I have \(favoriteGenres.count) favorite music genres")
if favoriteGenres.isEmpty {
print("As far as music goes, I am not picky")
} else {
print("I have particular music perferences")
}
修改集合
favoriteGenres.insert("Jazz")
favoriteGenres.remove("Rock")
查詢集合中n某個(gè)特定值
if favoriteGenres.contains("Funk") {
print("I get up on the good foot.")
} else {
print("It is too funky in here")
}
for genre in favoriteGenres {
print(genre)
}
for genre in favoriteGenres.sorted() {
print(genre)
}
基本集合操作
使用 intersection(:) 方法根據(jù)兩個(gè)集合中都包含的值創(chuàng)建的一個(gè)新的集合图毕。
使用 symmetricDifference(:) 方法根據(jù)在一個(gè)集合中但不在兩個(gè)集合中的值創(chuàng)建一個(gè)新的集合。
使用 union(:) 方法根據(jù)兩個(gè)集合的值創(chuàng)建一個(gè)新的集合眷唉。
使用 subtracting(:) 方法根據(jù)不在該集合中的值創(chuàng)建一個(gè)新的集合予颤。
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]
集合成員關(guān)系和相等
使用“是否相等”運(yùn)算符(==)來(lái)判斷兩個(gè)集合是否包含全部相同的值。
使用 isSubset(of:) 方法來(lái)判斷一個(gè)集合中的值是否也被包含在另外一個(gè)集合中厢破。
使用 isSuperset(of:) 方法來(lái)判斷一個(gè)集合中包含另一個(gè)集合中所有的值荣瑟。
使用 isStrictSubset(of:) 或者 isStrictSuperset(of:) 方法來(lái)判斷一個(gè)集合是否是另外一個(gè)集合的子集合或者父集合并且兩個(gè)集合并不相等。
使用 isDisjoint(with:) 方法來(lái)判斷兩個(gè)集合是否不含有相同的值(是否沒(méi)有交集)摩泪。
let houseAnimals: Set = ["??", "??"]
let farmAnimals: Set = ["??", "??", "??", "??", "??"]
let cityAnimals: Set = ["??", "??"]
houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isStrictSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
// true
字典
創(chuàng)建字典
// 創(chuàng)建空字典
var namesOfIntegers = [Int: String]()
namesOfIntegers[16] = "sixteen"
namesOfIntegers = [:]
// 字面量創(chuàng)建字典
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
var airports1 = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
訪問(wèn)字典
print("The dictionary of airports contains \(airports.count) items.")
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty")
}
增加字典元素
airports["LHR"] = "London"
修改字典元素
airports["LHR"] = "London Heathrow"
// updateValue(_:forKey:) 設(shè)置或者更新特定鍵對(duì)應(yīng)的值 存在該鍵則更新并返回原值 不存在則添加
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)")
} else {
print("That airport is not in the airports dictionary")
}
刪除字典中某元素
airports["APL"] = "Apple Internation"
airports["APL"] = 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")
}
字典遍歷
for (key, value) in airports {
print("key: \(key), value: \(value)")
}
for key in airports.keys {
print(key)
}
for value in airports.values {
print(value)
}
直接使用 keys 或者 values 屬性構(gòu)造一個(gè)新數(shù)組
let airportKeys = [String](airports.keys).sorted()
let airportValue = [String](airports.values).sorted()