數(shù)組 — Array
通過一個默認值重復創(chuàng)建數(shù)組
/// 創(chuàng)建一個重復元素為 0.0 且元素個數(shù)為 3 的數(shù)組
var threeDoubles = Array(repeating: 0.0, count: 3)
/// 創(chuàng)建一個重復元素為 0.1 且元素個數(shù)為 3 的數(shù)組
var anotherThreeDoubles = Array(repeating: 0.1, count: 3)
創(chuàng)建遞增數(shù)組
/// 新建一個 0 到 3 的遞增數(shù)組,每次遞增 1
var increasing = Array(0...3)
/// 新建一個 0 到 5 的遞增數(shù)組历恐,每次遞增 1
var increasingByOne = Array(stride(from: 0.0, through: 5.0, by: 1.0))
/// stride(from: through: by: ) 包括 from饺汹、through 兩個 element
/// stride(from: to: by: ) 包括 from, 但是不包括 to
通過兩個數(shù)組創(chuàng)建一個新的數(shù)組
var sixDoubles = threeDoubles + anotherThreeDoubles
通過字面量創(chuàng)建
var shoppingList: [String] = ["Eggs", "Milk"]
由于 Swift 的數(shù)據(jù)推斷橡伞,初始化的時候可以不加數(shù)據(jù)類型
var shoppingListNew = ["Eggs", "Milk"]
訪問和修改數(shù)組
shoppingList.count
shoppingList.isEmpty
shoppingList.append("Flour")
shoppingList += ["Baking Powder"]
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
var firstItem = shoppingList[0]
shoppingList[0] = "Six eggs"
shoppingList[4...6] = ["Bananas", "Apples"] // 4.5.6 -> 4.5
shoppingList.insert("Maple Syrup", at: 0)
var returnRemoveString = shoppingList.remove(at: 0)
var returnRemoveLast = shoppingList.removeLast()
遍歷
for item in shoppingList {
print(item)
}
同時獲取 index 和 item
for (index, item) in shoppingList.enumerated() {
print("Item \(index + 1): \(item)")
}
集合 — Set
存儲不同值并且相同類型檩赢、不排序
Hash Values for Set Types
- 類型必須是以 Hashable 以存儲在集合中昼蛀,Int 類型
- if a == b雀瓢,a.hashable == b.hashable
- 使用自定義類型作為集合值類型或者字典key類型梅掠,遵循 Hashable
- 遵循 Hashable 協(xié)議必須提供 get Int hashValue酌住,該值不需要在相同程序或者不同程序的不同執(zhí)行中相同
- Hashable 遵循 Equatable,所以還必須提供一個 == 操作符的實現(xiàn)
- == 必須滿足三個條件:
(1)a == a 自反性
(2)a == b 等效于 b == a
(3)a == b && b == c 等效于 a == c
創(chuàng)建和實例化空集合
var letters = Set<Character>()
letters.insert("a")
letters = []
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
// 不能從一個數(shù)組字面量被推斷為 Set 數(shù)據(jù)類型阎抒,必須顯示聲明
// but 不需要寫 set 中 的類型
let favoriteGenresNew: Set = ["Rock", "Classical", "Hip hop"]
訪問和修改集合
favoriteGenres.count
favoriteGenres.isEmpty
favoriteGenres.insert("Jazz")
if let removedGenre = favoriteGenres.remove("Rock") {
print("\(removedGenre)? I'm over it")
}else {
print("I never much cared for that")
}
if favoriteGenres.contains("Funk") {
print("I get up on the good foot.")
}else {
print("It's too funky in here")
}
遍歷集合
for genre in favoriteGenres {
print("\(genre)")
}
for genre in favoriteGenres.sorted() { // < 排序
print("\(genre)")
}
集合操作
var a: Set = Set<String>()
var b: Set = Set<String>()
a.intersection(b) // 交集酪我,共有部分
a.symmetricDifference(b) // 非共有部分總和
a.union(b) // 并集,a + b
a.subtracting(b) // a - a.intersection(b)(交集)
a == b // a 和 b 有所有相同值
a.isSubset(of: b) // a 是不是 b 的子集
a.isSuperset(of: b) // a 是不是 b 的父集
a.isStrictSubset(of: b) // a 是不是 b 的子集且叁,但是不等于 b
a.isStrictSuperset(of: b) // a 是不是 b 的父集都哭,但是不等于 b
a.isDisjoint(with: b) // a 和 b 沒有公共部分
字典 — Dictionary
var namesOfIntegers = [Int: String]() // 空數(shù)組
namesOfIntegers = [:] // 空數(shù)組
var airports: [String: String] = ["YYZ": "Toronto Person", "DUB": "Dublin"] // key: value
訪問和修改字典
airports.count
if airports.isEmpty {
print("The airports dictionary is empty")
} else {
print("The airports dictionary is not empty")
}
airports["LHR"] = "London"http:// 添加一個新值 或 修改值
var returnOldValue = airports.updateValue("London", forKey: "LHR") // 添加一個新值 或 修改值
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("The airport is not in the airports dictionary")
}
移除
airports["APL"] = nil
if let removedValue = airports.removeValue(forKey: "DUB") {
print("The removed airport's name is \(removedValue)")
} else {
print("The airport dictionary does not contain a value for DUB")
}
遍歷
for (airportCode, airportName) in airports {
print("\(airportCode) : \(airportName)")
}
// 取出 keys, values
for airportCode in airports.keys.sorted() {
print("Airport code: \(airportCode)")
}
for airportName in airports.values.sorted() {
print("Airport code: \(airportName)")
}
keys,values 初始化數(shù)組
let airportName = [String](airports.values)
let airportCode = [String](airports.keys)