Collection 分為 Arrays, Sets, Dictionaries(Collection源织,Sets有時都翻譯成集合)
1 數(shù)組(Arrays)是有序數(shù)據(jù)的集。
一個數(shù)組中只能有一種數(shù)據(jù)類型
var someInts = [Int]() // 創(chuàng)建一個由特定數(shù)據(jù)類型構(gòu)成的空數(shù)組
someInts = [] // someInts 現(xiàn)在是空數(shù)組豺撑,但是仍然是 [Int] 類型的瘟则。
let unmutableArray = [2, 3, 5] // 定義為let鱼响,數(shù)組內(nèi)容和數(shù)量就不能變化
var threeDoubles = [Double](repeating:0.0, count: 3) // `Array`創(chuàng)建特定大小并且所有數(shù)據(jù)都有相同默認(rèn)值的構(gòu)造方法
var anotherThreeDoubles = [Double](repeating: 2.5, count: 3)
var sixDoubles = threeDoubles + anotherThreeDoubles // 數(shù)組相加
sixDoubles.count
sixDoubles.isEmpty
sixDoubles.append(2.6)
sixDoubles += [3.2, 3.3]
sixDoubles[0]
sixDoubles[4...6] // 通過區(qū)間獲取多個數(shù)組項組成的新數(shù)組阱当,注意區(qū)間大小不能超過數(shù)組的下標(biāo)
sixDoubles.insert(4.4, at: 2)
sixDoubles.remove(at: 0)
sixDoubles.removeLast()
sixDoubles.removeAll()
// 數(shù)組遍歷
var shoppingList = ["Eggs", "Milk"]
for item in shoppingList {
print("\(item)")
}
for (index, value) in shoppingList.enumerated() { // enumerated()把數(shù)組的索引和值生成sequence用于遍歷
print("\(index): \(value)")
}
// 數(shù)組特殊操作
//var a = (1...10)
var arr = [1,3,7,11]
var arr2 = arr.map{$0 * 2}
var sum = arr.reduce(0,+)
// 驗證tweet中是否包含選定的若干關(guān)鍵字中的一個
let words = ["Swift","iOS","cocoa","OSX","tvOS"]
let tweet = "This is an example tweet larking about Swift"
//words.contains(tweet.containsString)
//words.filter({tweet.containsString($0)})
tweet.split(separator: " ")
.lazy
.map(String.init)
.contains(where: Set(words).contains)
let name = "andyron"
(1...4).forEach{print("Happy Birthday " + (($0 == 3) ? "dear \(name)":"to You"))}
// 創(chuàng)建重復(fù)固定長度數(shù)組
let arr3 = [Int?](repeating: nil, count: 20)
2 集合(Sets)是無序無重復(fù)數(shù)據(jù)的集石咬。
存在集合的中數(shù)據(jù)類型必須是 可哈峡模化(必須提供一個方法來計算它的哈希值, 相等的對象哈希值必須相同鬼悠, a.hashValue == b.hashValue
)
Swift的所有基本類型(比如String
, Int
, Double
和 Bool
)默認(rèn)都是可哈仙拘裕化的
在Swift內(nèi)部可哈希化的類型都接觸了Hashable
協(xié)議焕窝,它提高了一個屬性hashValue
var letters = Set<Character>() // 創(chuàng)建和構(gòu)造一個空的類型為`Set<Character>`的集合
letters.insert("a")
letters = []
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
var favoriteGenres2: Set = ["Rock", "Classical", "Hip hop"]
favoriteGenres.insert("Jazz")
favoriteGenres.count
favoriteGenres.remove("Classical") // 有成員就返回此成員蹬挺,否則返回`nil`
//favoriteGenres.removeAll()
favoriteGenres.contains("Rock")
// 遍歷集合
for genre in favoriteGenres {
print(genre)
}
for genre in favoriteGenres.sorted() {
print(genre)
}
// 集合操作
favoriteGenres = ["Rock", "Classical", "Hip hop"]
favoriteGenres2 = ["Hip hop", "R&B", "rap"]
favoriteGenres.intersection(favoriteGenres2) // 交集
favoriteGenres.symmetricDifference(favoriteGenres2) // 對稱差集
favoriteGenres.union(favoriteGenres2) // 并集
favoriteGenres.subtracting(favoriteGenres2) // 差集
favoriteGenres2.subtracting(favoriteGenres) // 差集
favoriteGenres.subtract(favoriteGenres2) // 區(qū)別于subtracting,沒有返回值直接修改favoriteGenres
var fg3: Set = ["rap"]
favoriteGenres == favoriteGenres2
favoriteGenres2.isSubset(of: fg3) // favoriteGenres2中所有元素是否在fg3中
favoriteGenres2.isSuperset(of: fg3) // favoriteGenres2是否包括fg3中所有元素
favoriteGenres2.isStrictSubset(of: fg3) // favoriteGenres2中所有元素是否在fg3中它掂,并且兩者不相同
favoriteGenres2.isStrictSuperset(of: fg3) // favoriteGenres2是否包括fg3中所有元素汗侵,并且兩者不相同
favoriteGenres.isDisjoint(with: fg3) // favoriteGenres與fg3是否沒有交集
3 字典(Dictionaries)是無序的鍵值對的集。
var namesOfIntegers = [Int: String]()
namesOfIntegers[16] = "sixteen"
namesOfIntegers = [:]
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
var airports2 = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
airports.count
airports.isEmpty
airports["hongqiao"] = "shanghai"
airports.updateValue("nanjing", forKey: "jichang")
airports["jichang"] = nil; //airports.remove(at: "jichang")
//airports.keys
//airports.values
// 遍歷
for (code, name) in airports {
print("\(code): \(name)")
}
for code in airports.keys {
print("\(code)")
}
playground文件在andyRon/LearnSwift