print("The shopping list contains \(shoppingList.count) items.") // 输出 "The shopping list contains 2 items."(这个数组有2个项)
使用布尔值属性isEmpty作为检查count属性的值是否为 0 的捷径:
1 2 3 4 5 6
if shoppingList.isEmpty { print("The shopping list is empty.") } else { print("The shopping list is not empty.") } // 打印 "The shopping list is not empty."(shoppinglist 不是空的)
var letters =Set<Character>() print("letters is of type Set<Character> with \(letters.count) items.") // 打印 "letters is of type Set<Character> with 0 items."
print("I have \(favoriteGenres.count) favorite music genres.") // 打印 "I have 3 favorite music genres."
使用布尔属性isEmpty作为一个缩写形式去检查count属性是否为0:
1 2 3 4 5 6
if favoriteGenres.isEmpty { print("As far as music goes, I'm not picky.") } else { print("I have particular music preferences.") } // 打印 "I have particular music preferences."
iflet removedGenre = favoriteGenres.remove("Rock") { print("\(removedGenre)? I'm over it.") } else { print("I never much cared for that.") } // 打印 "Rock? I'm over it."
使用contains(_:)方法去检查Set中是否包含一个特定的值:
1 2 3 4 5 6
if favoriteGenres.contains("Funk") { print("I get up on the good foot.") } else { print("It's too funky in here.") } // 打印 "It's too funky in here."
遍历一个集合
1 2 3 4 5 6
for genre in favoriteGenres { print("\(genre)") } // Classical // Jazz // Hip hop
1 2 3 4 5 6
for genre in favoriteGenres.sort() { print("\(genre)") } // prints "Classical" // prints "Hip hop" // prints "Jazz
基本集合操作
1 2 3 4 5 6 7 8 9 10 11 12
let oddDigits: Set= [1, 3, 5, 7, 9] let evenDigits: Set= [0, 2, 4, 6, 8] let singleDigitPrimeNumbers: Set= [2, 3, 5, 7]
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
访问和修改字典
我们可以通过字典的只读属性count来获取某个字典的数据项数量:
1 2
print("The dictionary of airports contains \(airports.count) items.") // 打印 "The dictionary of airports contains 2 items."(这个字典有两个数据项)
使用布尔属性isEmpty来快捷地检查字典的count属性是否等于0:
1 2 3 4 5 6
if airports.isEmpty { print("The airports dictionary is empty.") } else { print("The airports dictionary is not empty.") } // 打印 "The airports dictionary is not empty."
iflet oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") { print("The old value for DUB was \(oldValue).") } // 输出 "The old value for DUB was Dublin."
iflet removedValue = airports.removeValueForKey("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."
字典遍历
1 2 3 4 5
for (airportCode, airportName) in airports { print("\(airportCode): \(airportName)") } // YYZ: Toronto Pearson // LHR: London Heathrow
1 2 3 4 5 6 7 8 9 10 11
for airportCode in airports.keys { print("Airport code: \(airportCode)") } // Airport code: YYZ // Airport code: LHR
for airportName in airports.values { print("Airport name: \(airportName)") } // Airport name: Toronto Pearson // Airport name: London Heathrow
如果我们只是需要使用某个字典的键集合或者值集合来作为某个接受Array实例的 API 的参数,可以直接使用keys或者values属性构造一个新数组:
1 2 3 4 5
let airportCodes = [String](airports.keys) // airportCodes 是 ["YYZ", "LHR"]