美文网首页Swift
Swift 3.0之四、集合类型

Swift 3.0之四、集合类型

作者: 麟young | 来源:发表于2016-12-05 17:03 被阅读10次

Swift 提供了三种主要的集合类型:数组Array、合集Set还有字典Dictionary。数组是有序的值的集合,合集是唯一值的无序集合,字典是无序的键值对集合。赋值给变量的集合就是可变的。赋值给常量的集合是不可变的。

1. 数组

数组类型的完整写法为Array<Element>,Element是数组允许存入的值的类型,如Array<Int>,但常常用[Int]来代替此写法。

创建一个数组

var someInts = [Int]()
print("someInts is of type [Int] with \(someInts.count) items.")
// 结果为: "someInts is of type [Int] with 0 items."

创建好的数组可以添加元素或者被清空:

someInts.append(3)
// someInts 现在包含一个Int类型的元素
 
someInts = []
// someInts 现在被清空,但依然是[Int]类型

使用默认值创建数组

使用Array(repeating: count:)方法,自定义一定数量的默认值组成的数组:

var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles 是[Double]类型, 值为[0.0, 0.0, 0.0]

通过连接两个数组来创建数组

两个兼容类型的数组用加运算符(+)可创建一个新数组:

var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles 是[Double]类型, 值为 [2.5, 2.5, 2.5]
 
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles 被推断为[Double]类型, 值为 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

使用数组字面量创建数组

var shoppingList: [String] = ["Eggs", "Milk"]
// shoppingList 被初始化为含有两个字符串元素的数组

或者:

var shoppingList = ["Eggs", "Milk"]
// 虽然未写shoppingList类型,但由于所有元素都是String类型,所以数组被推断为[String]类型

访问和修改数组

要得出数组中元素的数量,使用count属性:

print("The shopping list contains \(shoppingList.count) items.")
// 结果为: "The shopping list contains 2 items."

使用isEmpty属性来检查count属性是否为0:

if shoppingList.isEmpty {
    print("The shopping list is empty.")
} else {
    print("The shopping list is not empty.")
}
// 结果为: "The shopping list is not empty."

使用append(_:)方法给数组末尾添加新的元素:

shoppingList.append("Flour")
// shoppingList 现在包含3个元素

或者使用"+="运算符来代替上面的方法:

shoppingList += ["Baking Powder"]
// shoppingList 现在包含4个元素
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList 现在包含7个元素

通过下标脚本语法来从数组当中取回一个值,在数组名后的方括号内传入你想要取回的值的索引:

var firstItem = shoppingList[0]
// firstItem 的值为 "Eggs"
// 注意: 数组的下标从0开始

使用下标脚本语法来改变给定索引中已经存在的值:

shoppingList[0] = "Six eggs"
// shoppingList数组第一个元素被更改为"Six eggs"

或者更改一个范围的值:

shoppingList[4...6] = ["Bananas", "Apples"]
// 将4、5、6三个元素替换为"Bananas", "Apples"两个元素
// shoppingList 现在包含6个元素

要把元素插入到特定的索引位置,调用数组的insert(_:at:)方法:

shoppingList.insert("Maple Syrup", at: 0)
// shoppingList 现在包含7个元素
// "Maple Syrup" 成为数组的第一个元素

使用remove(at:)方法来移除一个元素,并且返回这个元素值:

let mapleSyrup = shoppingList.remove(at: 0)
// 位置0的元素被移除
// shoppingList 现在包含6个元素, 不再拥有"Maple Syrup"
// mapleSyrup常量的值为 "Maple Syrup"

如果你想要移除数组最后一个元素,使用removeLast()方法而不是removeAtIndex(_:):

let apples = shoppingList.removeLast()
// 数组最后一个元素被移除
// shoppingList 现在包含5个元素,不再拥有 apples
// apples 常量的值为 "Apples"

遍历一个数组

使用for...in来遍历整个数组:

for item in shoppingList {
    print(item)
}
// Six eggs
// Milk
// Flour
// Baking Powder
// Bananas

如果需要遍历每个元素的索引和值,使用enumerated()方法:

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

2. 合集

合集:同一类型,值不重复,无序地储存的集合

Set类型的哈希值

哈希值是Int值,若a == b,意味着 a.hashValue == b.hashValue。

创建并初始化一个空合集

var letters = Set<Character>()
// 和数组不一样,此处没有简写
print("letters is of type Set<Character> with \(letters.count) items.")
// 结果为: "letters is of type Set<Character> with 0 items."

创建好的合集可以添加元素或者被清空:

letters.insert("a")
// letters 现在包含一个字符元素
letters = []
// letters 现在为空,但依然是Set<Character>类型

使用数组字面量创建合集

var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
// favoriteGenres has been initialized with three initial items

或者简写为:

var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
// favoriteGenres被推断为Set<String>类型

访问和修改合集

要得出合集中元素的数量,使用count属性:

print("I have \(favoriteGenres.count) favorite music genres.")
// 结果为: "I have 3 favorite music genres."

使用isEmpty属性作为检查count属性是否为0:

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."

使用insert(_:)方法来添加一个新的元素到合集:

favoriteGenres.insert("Jazz")
// favoriteGenres 现在包含4个元素

调用合集的remove(_:)方法移除一个元素,如果元素是合集的元素就移除它并返回被移除的值,如果合集没有这个元素就返回 nil。另外:合集当中所有的元素可以用removeAll()一次移除:

if let removedGenre = favoriteGenres.remove("Rock") {
    print("\(removedGenre)? I'm over it.")
} else {
    print("I never much cared for that.")
}
// 结果为: "Rock? I'm over it."

检查合集是否包含特定元素,使用contains(_:)方法:

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."

遍历合集

使用for...in语句遍历:

for genre in favoriteGenres {
    print("\(genre)")
}
// Classical
// Jazz
// Hip hop

Set类型是无序的,使用sorted()方法将其变为有序数组:

for genre in favoriteGenres.sorted() {
    print("\(genre)")
}
// Classical
// Hip hop
// Jazz

合集与合集之间的操作

  • 使用 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]

合集成员关系和相等性

  • 使用“相等”运算符 ( == )来判断两个合集是否包含有相同的值
  • 使用 isSubset(of:) 方法来确定一个合集的所有值是被某合集包含
  • 使用 isSuperset(of:)方法来确定一个合集是否包含某个合集的所有值
  • 使用 isStrictSubset(of:) 或者 isStrictSuperset(of:)方法来确定是否为某一个合集的子集或者超集,但并不相等
  • 使用 isDisjoint(with:)方法来判断两个合集是否拥有相同的值。举个栗子:
let houseAnimals: Set = ["🐶", "🐱"]
let farmAnimals: Set = ["🐮", "🐔", "🐑", "🐶", "🐱"]
let cityAnimals: Set = ["🐦", "🐭"]
 
houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
// true

3. 字典

字典类型的完整写法为Dictionary<Key, Value>,通常简写为[Key: Value],如[Int, String]

创建一个空字典

var namesOfIntegers = [Int: String]()
// namesOfIntegers 是一个空的[Int: String]类型的字典

修改字典:

namesOfIntegers[16] = "sixteen"
// namesOfIntegers 现在包含一个键值对
namesOfIntegers = [:]
// namesOfIntegers 被置空,但仍然是[Int: String]类型

用字典字面量创建字典

var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]

或者简写为:

var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
// airports 被推断为 [String: String]类型

访问和修改字典

使用count只读属性找出字典键值对数量:

print("The airports dictionary contains \(airports.count) items.")
// 结果为: "The airports dictionary contains 2 items."

使用isEmpty属性检查count属性是否为0:

if airports.isEmpty {
    print("The airports dictionary is empty.")
} else {
    print("The airports dictionary is not empty.")
}
// 结果为: "The airports dictionary is not empty."

用下标脚本给字典添加或修改元素:

airports["LHR"] = "London"
// airports现在包含三个键值对 
airports["LHR"] = "London Heathrow"
// 字典中"LHR"对应的值更改为"London Heathrow"

使用updateValue(_:forKey:)方法修改字典后,返回修改前的值,如果没有修改前的值则返回nil:

if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
    print("The old value for DUB was \(oldValue).")
}
// 结果为: "The old value for DUB was Dublin."

如果字典包含了请求的键,下标脚本就返回对应的值。否则,下标脚本返回 nil:

if let airportName = airports["DUB"] {
    print("The name of the airport is \(airportName).")
} else {
    print("That airport is not in the airports dictionary.")
}
// 结果为: "The name of the airport is Dublin Airport."

使用下标脚本语法给一个键赋值nil来从字典当中移除一个键值对:

airports["APL"] = "Apple International"
airports["APL"] = nil
// APL 这个键被删除

另外,使用removeValue(forKey:)移除键值对。如果键存在,移除并且返回移对应值,如果键不存在返回nil:

if let removedValue = airports.removeValue(forKey: "DUB") {
    print("The removed airport's name is \(removedValue).")
} else {
    print("The airports dictionary does not contain a value for DUB.")
}
// 结果为: "The removed airport's name is Dublin Airport."

遍历字典

使用for...in语句遍历字典的键值对。将字典中的每一个元素拆为(key, value)元组:

for (airportCode, airportName) in airports {
    print("\(airportCode): \(airportName)")
}
// YYZ: Toronto Pearson
// LHR: London Heathrow

同样,通过keysvalues属性访问键或值的集合:

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

获取键或值的集合:

let airportCodes = [String](airports.keys)
// airportCodes 的值为 ["YYZ", "LHR"]
let airportNames = [String](airports.values)
// airportName 的值为 ["Toronto Pearson", "London Heathrow"]

相关文章

  • Swift 3.0之四、集合类型

    Swift 提供了三种主要的集合类型:数组Array、合集Set还有字典Dictionary。数组是有序的值的集合...

  • Swift3.0之集合类型

    一、数组通过初始化语法创建一个特定类型的空数组 如果上文已提供类型信息,像函数参数或者已有类型的常变量,可以使用空...

  • Swift3.0 - 属性

    Swift3.0 - 真的很简单Swift3.0 - 数据类型Swift3.0 - ArraySwift3.0 -...

  • Swift3.0 - 镜像

    Swift3.0 - 真的很简单Swift3.0 - 数据类型Swift3.0 - ArraySwift3.0 -...

  • Swift3.0 - 真的很简单

    Swift3.0 - 真的很简单Swift3.0 - 数据类型Swift3.0 - ArraySwift3.0 -...

  • Swift3.0 - Array

    Swift3.0 - 真的很简单Swift3.0 - 数据类型Swift3.0 - ArraySwift3.0 -...

  • Swift3.0 - 泛型

    Swift3.0 - 真的很简单Swift3.0 - 数据类型Swift3.0 - ArraySwift3.0 -...

  • Swift3.0 - 数据类型

    Swift3.0 - 真的很简单Swift3.0 - 数据类型Swift3.0 - ArraySwift3.0 -...

  • Swift3.0 - 函数和闭包

    Swift3.0 - 真的很简单Swift3.0 - 数据类型Swift3.0 - ArraySwift3.0 -...

  • Swift3.0 - 黑魔法swizzle

    Swift3.0 - 真的很简单Swift3.0 - 数据类型Swift3.0 - ArraySwift3.0 -...

网友评论

    本文标题:Swift 3.0之四、集合类型

    本文链接:https://www.haomeiwen.com/subject/ejukmttx.html