Swift 提供来三种集合类型,即Array、Set、Dictionary。Array是值的有序集合;Set是无重复值的无序集合;Dictionary是键值对的无序集合

在Swift中,所有存储在Array、Set、Dictionary中的值或者键都是有类型的。这意味着不能将与集合数据类型不符的数据加入到集合中。同样也意味着你从集合获得的值都会有一个确定的类型
注:Swift中,Array、Set、Dictionary都实现为范型集合。更多关于范型类型与集合的内容,参看范型
集合可变性 Mutability of Collections
如果创建一个Array、Set、或Dictionary,并赋值给一个变量,则创建的集合就是可以修改的。这意味着在创建集合后,可以通过添加、删除、修改的方式改变集合里的项目。如果将一个Array、Set、或Dictionary赋值给常量,集合就不可更改,并且集合的大小和内容都不可以修改
注:使用集合时,如果不需要改变,创建不可更变的集合是推荐的用法。而且,使用不可变集合时,Swift的编译器也会针对其进行性能优化
Arrays
Array将相同类型的值存储在顺序列表中。相同的值可以在不同的位置多次出现
注:
Swift的Array可与Foundation的NSArray类型桥接。
更多关于使用Foundation和Cocoa的Array的信息,参看Using Swift with Cocoa and Objective-C
简写语法 Array Type Shorthand Syntax
Swift的Array的全写形式为Array<T>
,T
就是Array可存放的数据类型。简写形式为 [T]
。虽然两者写法功能相同,但首选简写的方式,在本指南中使用Array地方,通常也都会使用简写的形式
创建空数组 Creating an Empty Array
创建某一类型的空Array时,可使用初始化语法:
<pre><code>
var someInts = [Int]()
print("someInts is of type [Int] with \(someInts.count) items.")
// prints "someInts is of type [Int] with 0 items."
</code></pre>
注意,根据初始化方法的类型,someInts
变量的类型被推断为 [Int]
另外,如果上下文已经提供了类型信息,如函数参数,或者已经有类型的常量或变量,则可以使用空Array写法([]
,一对空的方括号)创建一个空Array:
<pre><code>
someInts.append(3)
// someInts now contains 1 value of type Int
someInts = []
// someInts is now an empty array, but is still of type [Int]
</code></pre>
使用缺省值创建数组 Creating an Array with a Default Value
Swift的 Array 提供的一个创建指定大小数组、并且其所有值都相同的初始化方法。需要给初始化方法传入两个参数:数组大小(count
)以及适当类型的缺省值(repeatedValue
):
<pre><code>
var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
// threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]
</code></pre>
将两个数组合成一个数组 Creating an Array by Adding Two Arrays Together
使用(+
)操作符,可以将两个类型兼容的数组合并在一起,生成一个新数组
<pre><code>
var anotherThreeDoubles = [Double](count: 3, repeatedValue: 2.5)
// anotherThreeDoubles is inferred as [Double], and equals [2.5, 2.5, 2.5]
<br />
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
</code></pre>
在代码中写出数组 Creating an Array with an Array Literal
还有一个比较简便的方式,如果数组有一个或几个值时,可以直接将数组以如下形式写在代码里:
[value 1
, value 2
, value 3
]
例如:
<pre><code>
var shoppingList: [String] = ["Eggs", "Milk"]
// shoppingList has been initialized with two initial items
</code></pre>
注:这里把
shoppingList
声明为变量而不是常量,是因为下面其他的例子将会在数组内增加内容
本例,数组代码中只有两个字符串,这与数组变量声明时的类型一致,所以可以使用赋值的方式初始化数组
借助于Swift的类型推断,在初始化数组时,如果所有子项的类型都一样,可以省去数组声明中的类型信息。由此可将上例改为:
<pre><code>
var shoppingList = ["Eggs", "Milk"]
</code></pre>
因为所有子项的类型相同,Swift可以推断出数组 shoppingList
的类型为[String]
。
访问与修改 Accessing and Modifying an Array
可以通过数组的方法和属性访问、修改数组内容,或者通过下标语法
要知道数组内有多少子项,通过只读属性count
就看获得:
<pre><code>
print("The shopping list contains (shoppingList.count) items.")
// prints "The shopping list contains 2 items."
</code></pre>
通过布尔属性 isEmpty
可以简便的确认数组的子项数是否为0:
<pre><code>
if shoppingList.isEmpty {
print("The shopping list is empty.")
} else {
print("The shopping list is not empty.")
}
// prints "The shopping list is not empty."
</code></pre>
调用方法 append(_:)
,可以在数组尾部增加子项:
<pre><code>
shoppingList.append("Flour")
// shoppingList now contains 3 items, and someone is making pancakes
</code></pre>
或者,使用(+=
)操作符在数组尾部增加其他类型兼容的数组:
<pre><code>
shoppingList += ["Baking Powder"]
// shoppingList now contains 4 items
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList now contains 7 items
</code></pre>
使用下标语法检索数组,需要在紧接着变量名后的方括号中传入索引值:
<pre><code>
var firstItem = shoppingList[0]
// firstItem is equal to "Eggs"
</code></pre>
注:第一个子项的索引开始于0,而不是1。Swift中数组的索引总是从0开始的
使用下标语法,可以修改数组指定索引位置的信息
<pre><code>
shoppingList[0] = "Six eggs"
// the first item in the list is now equal to "Six eggs" rather than "Eggs"
</code></pre>
使用下标语法,还可以一次性修改数组一定范围内的内容,甚至替换数组的大小与范围的大小不同时,也能正常工作。如下例:
<pre><code>
shoppingList[4...6] = ["Bananas", "Apples"]
// 现在shoppingList还有6项,原来是7项,因为替换数组没有对应的值所有讲第七项(索引为6)删除了
</code></pre>
注:不能使用下标语法在数组后增加新内容(You can’t use subscript syntax to append a new item to the end of an array)
但是下面的代码却能执行。这是Swift 2.0带来的改变吗?
<pre><code>
for item in shoppingList {
print("\(item), ", appendNewline: false)
}
print("")
// Eggs, Milk, Flour, Baking Powder, Chocolate Spread, Cheese, Butter
<br />
shoppingList[6...6] = ["Bananas", "Apples"]
for item in shoppingList {
print("\(item), ", appendNewline: false)
}
print("")
// Eggs, Milk, Flour, Baking Powder, Chocolate Spread, Cheese, Bananas, Apples,
<br />
shoppingList[7...7] += ["Tests"]
for item in shoppingList {
print("\(item), ", appendNewline: false)
}
print("")
// Eggs, Milk, Flour, Baking Powder, Chocolate Spread, Cheese, Bananas, Apples, Tests,
</code></pre>
如果要在一个位置插入一个子项,使用方法insert(_:atIndex:)
<pre><code>
shoppingList.insert("Maple Syrup", atIndex: 0)
// shoppingList now contains 9 items
// "Maple Syrup" is now the first item in the list
</code></pre>
现在 Maple Syrup
占据了 shoppingList
索引为0的位置
类似的,删除数组的一个子项,需要使用方法removeAtIndex(_:)
。他会删除指定索引位置的子项,并将子项返回(如果不需要返回值,可以忽略):
<pre><code>
let mapleSyrup = shoppingList.removeAtIndex(0)
// the item that was at index 0 has just been removed
// shoppingList now contains 8 items, and no Maple Syrup
// the mapleSyrup constant is now equal to the removed "Maple Syrup" string
</code></pre>
注:如果访问、修改数组的索引值越界(超过数组的边界),将会触发运行时错误。所以在访问、修改前,先通过与数组的
count
比较来检查索引值是否合法。除非数组为空(count
为 0),否则最大的有效索引值为count - 1
,因为数组的索引是从 0 开始的
进行删除时,空下的位置会被接下来的子项替代,所以不会产生空隙:
<pre><code>
firstItem = shoppingList[0]
// firstItem is now equal to "Eggs"
</code></pre>
要删除数组的最后一项,使用方法removeLast()
要比方法removeAtIndex(_:)
好,因为可以避免查询 count
属性。与removeAtIndex(_:)
,removeLast()
也返回被删除打子项
<pre><code>
let tests = shoppingList.removeLast()
// the last item in the array has just been removed
// shoppingList now contains 7 items, and no apples
// the apples constant is now equal to the removed "Apples" string
</code></pre>
遍历数组 Iterating Over an Array
可以使用for - in
循环语句变量数组的所有值:
<pre><code>
for item in shoppingList {
print(item)
}
// Eggs
// Milk
// Flour
// Baking Powder
// Chocolate Spread
// Cheese
// Bananas
// Apples
</code></pre>
如果索引值和子项都需要,使用 enumerate()
方法遍历。enumerate()
方法返回一个由每个子项的索引和值构成的元组的数组。可以将元组分解到临时的常量或变量中,如下:
<pre><code>
for (index, value) in shoppingList.enumerate() {
print("Item \(index + 1): \(value)")
}
// Item 1: Eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Chocolate Spread
// Item 6: Cheese
// Item 7: Bananas
// Item 8: Apples
</code></pre>
Sets
集合用于存放同类型的不重复的值,其内存放数据没有顺序。如果对于数据没有顺序要求,或者需要保证没有重复项,则可以使用集合来替代数组
注:Swift的Set与Foundation的NSSet桥接(bridge)
Hash Values for Set Types
为了将数据存入集合,其类型必须支持哈希。这就是说,类型必须提供计算自己哈希值的方法。哈希值的类型为Int,对所有对象进行相等比较, if a == b
与 a.hashValue == b.hashValue
是一样的。
所有Swift的基本类型都是默认支持哈希的,可以用于集合的值类型与字典的键(key)类型。在Enumeration中,如果其内的值没有其他相关值(参考Enumerations),默认也是支持哈希的
注:想要使自定义的类型作为集合的值类型或者字典的键类型,需要其遵从 Swift 标准库的
Hashable
协议。遵从Hashable
协议的类型,必须提供一个返回Int
类型数据的属性 ——hashValue
。属性hashValue
的返回值,在不同程序中,或者统一个程序的不同实例中,可以不同(换言之,在同一个程序点同一个实例中,每次返回值必须相同)
<br />
因为Hashable
协议遵从Equatable
协议,所以遵从Hashable
协议的类型必须提供一个等于比较操作符(==
)的实现。Equatable
协议需要任何遵从本协议的类型实现的(==
)操作符是等价关系。也就是说,对于值a
,b
,c
来说,相等比较操作符(==
)必须满足下面三个条件:
-
a == a
(反身性——Reflexivity) -
a == b
意味着b == a
(对称性——Symmetry) -
a == b && b == c
意味着a == c
(传递性——Transitivity)
跟多相关信息请参考Protocols
Set Type Syntax
使用集合类型的写法为 Set<T>
。其中,T
是可以存入集合的值的类型。与数组(例如:let array = [String]()
)不同,集合没有简写的方式
Creating and Initializing an Empty Set
创建一个空集合的方法如下:
<pre><code>
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."
</code></pre>
如果已经知道集合的类型,例如是函数的参数,或已经知道其类型常量或变量,可以用如下方法将创建一个空集合,就像数组一样:
<pre><code>
letters.insert("a")
// letters now contains 1 value of type Character
letters = []
// letters is now an empty set, but is still of type Set<Character>
</code></pre>
Creating a Set with an Array Literal
像初始化数组一样,有如下简便的方式,使用一个或多个值初始化集合:
<pre><code>
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
// favoriteGenres has been initialized with three initial items
</code></pre>
注:这里使用
var
声明favoriteGenres
,是因为下面的其他例子中需要改变其值
因为集合类型不能单从其使用数组写法来推断,所以集合类型必须显示声明。不管,由于Swift的类型推断,在使用一组相同类型的数据,以数组的写法初始化集合时,可以不用明确写出集合的类型。所以,集合favoriteGenres
也可以使用如下写法初始化:
<pre><code>
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
</code></pre>
集合favoriteGenres的类型推断为:Set<String>
Accessing and Modifying a Set
使用集合的方法和属性访问、修改其内容
获得集合内的子项数量,使用只读属性count
:
<pre><code>
print("I have \(favoriteGenres.count) favorite music genres.")
// prints "I have 3 favorite music genres."
</code></pre>
使用布尔属性isEmpty
,可以方便的判断集合内的子项数量是否为0:
<pre><code>
if favoriteGenres.isEmpty {
print("As far as music goes, I'm not picky.")
} else {
print("I have particular music preferences.")
}
// prints "I have particular music preferences."
</code></pre>
使用insert(_:)
方法加入新的子项:
<pre><code>
favoriteGenres.insert("Jazz")
// favoriteGenres now contains 4 items
</code></pre>
使用remove(_:)
方法删除特定的子项,并且返回被删除打值,或者子项不存在,则返回nil。使用removeAll()
方法删除所有的子项:
<pre><code>
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."
</code></pre>
使用contains(_:)
方法检查集合内是否存在特定的值
<pre><code>
if favoriteGenres.contains("Funk") {
print("I get up on the good foot.")
} else {
print("It's too funky in here.")
}
// prints "It's too funky in here."
</code></pre>
Iterating Over a Set
可以使用for - in
循环遍历集合内的所有值:
<pre><code>
for genre in favoriteGenres {
print("\(genre)")
}
// Classical
// Jazz
// Hip hop
</code></pre>
Swift的集合类型没有排序,如果想按特定顺序遍历集合,则使用sort()
方法,返回一个排序后的集合(Array):
<pre><code>
for genre in favoriteGenres.sort() {
print("\(genre)")
}
// Classical
// Hip hop
// Jazz
</code></pre>
Performing Set Operations
Swift提供了高效的基础集合操作,如果合并两个集合,查看两个集合的公共内容,或者查明两个集合是否相同,或部分相同、或完全不同
Fundamental Set Operations
下图描述两个集合 a
,b
,在各种集合操作后的结果,使用阴影区域表示

-
intersect(_:)
,返回两个集合的交集 -
exclusiveOr(_:)
,返回两个集合的非交集 -
union(_:)
,返回两个集合的并集 -
subtract(_:)
,返回两个集合的差集
<pre><code>
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]
<br />
oddDigits.union(evenDigits).sort()
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersect(evenDigits).sort()
// []
oddDigits.subtract(singleDigitPrimeNumbers).sort()
// [1, 9]
oddDigits.exclusiveOr(singleDigitPrimeNumbers).sort()
// [1, 2, 9]
</code></pre>
Set Membership and Equality
下图描述三个集合a
,b
,c
,并用重叠的区域表示他们之间共有的元素。集合 a
是集合 b
的超集。反之,集合 b
是集合 a
的子集,因为所有集合 b
的元素都包含在集合 a
中。集合 b
与 c
不相交,因为他们没有公共的元素

-
==
,判断两个集合是否完全相同 -
isSubsetOf(_:)
,判断是否是另一个集合的子集 -
isSupersetOf(_:)
, 判断是否为另一个集合的超级 -
isStrictSubsetOf(_:)
orisStrictSupersetOf(_:)
,是非为另一个集合的超集或自己,但是不完全相等 -
isDisjointWith(_:)
,判断两个集合是否有相同的元素
<pre><code>
let houseAnimals: Set = ["🐶", "🐱"]
let farmAnimals: Set = ["🐮", "🐔", "🐑", "🐶", "🐱"]
let cityAnimals: Set = ["🐦", "🐭"]
<br />
houseAnimals.isSubsetOf(farmAnimals)
// true
farmAnimals.isSupersetOf(houseAnimals)
// true
farmAnimals.isDisjointWith(cityAnimals)
// true
</code></pre>
Dictionaries
字典,保存关联的键-值对的无序集合。其中所有的键都为同一个类型,所有值也为同一个类型。每个值都有唯一的键,用于在字典中唯一表示其对应的值。与数组不同,字典中的子项是无序的。在需要通过标示符得到其对应值时,使用字典,就像现实世界中使用字典查找单词一样。
注:Swift中的
Dictionary
与 Foundation 中的NSDictionary
类型桥接(bridge)
Dictionary Type Shorthand Syntax
Swift中,字典使用形式为Dictionary<Key, Value>
,key
表示能被用于字典键值的类型,value
是字典存储的值的类型,其与键值一一对应
注:字典的
key
,必须遵从Hashable
协议,与Set相似
字典的简写方式为 [Key: Value]
,虽然两种形式功能相同,但是首选简写的形式,并且本指南基本都用简写的形式
Creating an Empty Dictionary
与数组类似,可以用如下方法初始化空字典:
<pre><code>
var namesOfIntegers = Int: String
// namesOfIntegers is an empty [Int: String] dictionary
</code></pre>
如果事先知道字典类型,则可以用如下方式初始化空字典:
<pre><code>
namesOfIntegers[16] = "sixteen"
// namesOfIntegers now contains 1 key-value pair
namesOfIntegers = [:]
// namesOfIntegers is once again an empty dictionary of type [Int: String]
</code></pre>
Creating a Dictionary with a Dictionary Literal
也可以在编写代码时,直接构建字典,写法与数组类似。形式如下:
[key 1
: value 1
, key 2
: value 2
, key 3
: value 3
]
具体的例子如下:
<pre><code>
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
</code></pre>
同样,通过Swift的类型推断,上例可以写为:
<pre><code>
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
</code></pre>
Accessing and Modifying a Dictionary
可以使用字典的方法,属性访问、修改字典,同样也可以使用下标语法
如同使用数组,获取字典的子项数量,使用只读属性 count
:
<pre><code>
print("The airports dictionary contains \(airports.count) items.")
// prints "The airports dictionary contains 2 items."
</code></pre>
使用布尔属性isEmpty
,可以方便的判断字典内的子项数量是否为0
<pre><code>
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
// prints "The airports dictionary is not empty."
</code></pre>
使用下标语法给字典增加内容,使用适当的key作为下标索引,并赋值适当类型的值:
<pre><code>
airports["LHR"] = "London"
// the airports dictionary now contains 3 items
</code></pre>
同样,修改也可以使用下标:
<pre><code>
airports["LHR"] = "London Heathrow"
// the value for "LHR" has been changed to "London Heathrow"
</code></pre>
或者,使用updateValue(_:forKey:)
方法增加或更新字典的内容。与下标方式类似,如果key不存在,则增加新到子项。如果key存在则更新其值为新的值。与下标语法不通的是,使用updateValue(_:forKey:)
进行更新,将会返回更新前key对应的值,由此可以坚持更新是否成功
updateValue(_:forKey:)
方法返回Optional值。例如,如果字典存放String
值,则该方法返回String?
。如果对应的key此前有值,则返回其值;无值,则返回nil
<pre><code>
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."
</code></pre>
使用下标语法,还可以通过key查询其值。因为查询不存在的key也可以执行,所以下标语法返回Optional值。如果key存在,就返回其Optional值,否则返回nil
:
<pre><code>
if let airportName = airports["DUB"] {
print("The name of the airport is \(airportName).")
} else {
print("That airport is not in the airports dictionary.")
}
// prints "The name of the airport is Dublin Airport."
</code></pre>
使用下标语法,通过给字典对应的key赋值nil,可以删除相关的键-值对:
<pre><code>
airports["APL"] = "Apple International"
// "Apple International" is not the real airport for APL, so delete it
airports["APL"] = nil
// APL has now been removed from the dictionary
</code></pre>
或者,也可以使用removeValueForKey(_:)
方法删除key对应的键-值对。如果key存在,则返回删除的值,否则返回nil
:
<pre><code>
if let 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."
</code></pre>
Iterating Over a Dictionary
可以使用for - in
语句遍历字典中所有的键-值对。此过程中,字典中的每个子项都是元组(key, value
),并且可以分解其子值到常量或变量中:
<pre><code>
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
// YYZ: Toronto Pearson
// LHR: London Heathrow
</code></pre>
还可以使用字典的属性keys
和 values
(各自返回可遍历的key 和 value的集合)得到所有的键和值:
<pre><code>
for airportCode in airports.keys {
print("Airport code: \(airportCode)")
}
// Airport code: YYZ
// Airport code: LHR
<br />
for airportName in airports.values {
print("Airport name: \(airportName)")
}
// Airport name: Toronto Pearson
// Airport name: London Heathrow
</code></pre>
如果需要以数组对形式使用字典的keys 或 values,可以使用如下方法初始化keys 和 values 的数组:
<pre><code>
let airportCodes = [String](airports.keys)
// airportCodes is ["YYZ", "LHR"]
<br />
let airportNames = [String](airports.values)
// airportNames is ["Toronto Pearson", "London Heathrow"]
</code></pre>
网友评论