集合类型
Swift提供了三种主要的集合类型,称为Array,Set和Dictionary,用于存储值集合。Array是有序的值集合。Set是唯一值的无序集合。Dictionary是键值关联的无序集合。
注意
Swift的数组,集合和字典类型实现为泛型集合。有关泛型类型和集合的更多信息,请参阅泛型,Array<Element>、Set<Element>、Dictionary<Element>。
一、数组
一个阵列存储在有序列表中的相同类型的值。相同的值可以在不同位置多次出现在数组中。
1、数组初始化
var someInts = [Int]()
print("someInts is of type [Int] with \(someInts.count) items.")
// Prints "someInts is of type [Int] with 0 items."
someInts.append(3)
// someInts now contains 1 value of type Int
someInts = []
// someInts is now an empty array, but is still of type [Int]
2、创建具有默认值的数组
var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]
3、访问和修改数组
print("The shopping list contains \(shoppingList.count) items.")
// Prints "The shopping list contains 2 items."
if shoppingList.isEmpty {
print("The shopping list is empty.")
} else {
print("The shopping list is not empty.")
}
// Prints "The shopping list is not empty."
shoppingList.append("Flour")
// shoppingList now contains 3 items, and someone is making pancakes
shoppingList += ["Baking Powder"]
// shoppingList now contains 4 items
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList now contains 7 items
4、遍历数组
var shoppingList = ["Eggs", "Milk","Flour","Baking Powder","Bananas"]
for item in shoppingList {
print(item)
}
// Six eggs
// Milk
// Flour
// Baking Powder
// Bananas
for (index, value) in shoppingList.enumerated() {
print("Item \(index + 1): \(value)")
}
// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Bananas
二、Set 集合
Set集合存储相同类型的不同值,没有定义的顺序。当项目的顺序不重要时,或者当您需要确保项目仅出现一次时,您可以使用集合而不是数组
var letters = Set<Character>()
print("letters is of type Set<Character> with \(letters.count) items.")
// Prints "letters is of type Set<Character> with 0 items."
//是否为空
if favoriteGenres.isEmpty {
print("As far as music goes, I'm not picky.")
} else {
print("I have particular music preferences.")
}
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
//删除
if let removedGenre = favoriteGenres.remove("Rock") {
print("\(removedGenre)? I'm over it.")
} else {
print("I never much cared for that.")
}
// Prints "Rock? I'm over it."
//遍历
for genre in favoriteGenres {
print("\(genre)")
}
// Classical
// Jazz
// Hip hop
基本集合运算
下图描绘了两个集- a和b-附由阴影区域表示的各种设定操作的结果
- 使用该intersection(_:)方法创建一个只包含两个组共有的值的新集。
- 使用该symmetricDifference(_:)方法创建一个新集合,其中包含任一集合中的值,但不能同时创建两者。
- 使用该union(_:)方法创建包含两个集中所有值的新集。
- 使用此subtracting(_:)方法创建一个值不在指定集中的新集。
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]
三、字典
字典存储相同类型的密钥和一个集合中的相同类型的值与没有定义排序之间的关联。每个值都与唯一键相关联,该唯一键充当字典中该值的标识符。
var namesOfIntegers = [Int: String]()
// namesOfIntegers is an empty [Int: String] dictionary
namesOfIntegers[16] = "sixteen"
// namesOfIntegers now contains 1 key-value pair
namesOfIntegers = [:]
// namesOfIntegers is once again an empty dictionary of type [Int: String]
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
// Prints "The airports dictionary is not empty."
updateValue(_:forKey:)如果不存在,则该方法为键设置值,或者如果该键已存在则更新该值
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
print("The old value for DUB was \(oldValue).")
}
// Prints "The old value for DUB was Dublin."
// Dictionary 遍历 (key, value)
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
// YYZ: Toronto Pearson
// LHR: London Heathrow
网友评论