Swift5.5学习笔记三:集合(数组Array,集合Set,字典Dictionary)

//Collection Types
//Swift 提供了三种主要的集合类型,称为数组、集合和字典,用于存储值的集合。
//数组是有序的值集合。集合是唯一值的无序集合。字典是键值关联的无序集合。

//一、集合
//如果您创建一个数组、一个集合或一个字典,并将其分配给一个变量,则创建的集合将是可变的。
//这意味着您可以在集合创建后通过添加、删除或更改集合中的项目来更改(或变异)集合。
//如果将数组、集合或字典分配给常量,则该集合是不可变的,并且无法更改其大小和内容。

//二、数组(Array)
//1.数组类型速记语法
//Swift 数组的类型全写为Array<Element>,其中Element是数组允许存储的值的类型。
//您还可以将数组的类型以简写形式写为[Element].
//尽管这两种形式在功能上是相同的,首选缩写形式使用。

//2.创建一个空数组

var someInts: [Int] = []

// Prints "someInts is of type [Int] with 0 items."
print("someInts is of type [Int] with \(someInts.count) items.")

//3.使用默认值创建数组
//Swift的Array类型还提供了一个初始化器,用于创建一个特定大小的数组,并将其所有值设置为相同的默认值。
//向这个初始值传递一个适当类型的默认值(称为repeating),以及该值在新数组中重复的次数(称为count)即完成初始化

// threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]
var threeDoubles = Array(repeating: 0.0, count: 3)

//4.通过将两个数组相加来创建数组
//您可以通过使用加法运算符(+)将具有兼容类型的两个现有数组相加来创建新数组。
//新数组的类型是从您相加的两个数组的类型推断出来的

// anotherThreeDoubles is of type [Double], and equals [2.5, 2.5, 2.5]
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)

// sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
var sixDoubles = threeDoubles + anotherThreeDoubles

//5.使用数组字面量创建数组
//您还可以使用数组字面量初始化数组,这是将一个或多个值写入数组集合的一种简写方式。数组字面量被写成一个值列表,用逗号分隔,用一对方括号括起来:

//[value 1, value 2, value 3]

//shoppingList has been initialized with two initial items
//var shoppingList: [String] = ["Eggs", "Milk"]

//由于 Swift 的类型推断,如果您使用包含相同类型值的数组字面量初始化数组,则不必编写数组的类型。shoppingList的初始化可以写成更短的形式:

var shoppingList = ["Eggs", "Milk"]

//6.访问和修改数组
//您可以通过数组的方法和属性或使用下标语法来访问和修改数组。

//要找出数组中的项数,请检查其只读count属性:

// Prints "The shopping list contains 2 items."
print("The shopping list contains \(shoppingList.count) items.")

//使用isEmpty属性作为检查count属性是否等于0的快捷方式:
//Prints "The shopping list isn't empty."
if shoppingList.isEmpty {
    print("The shopping list is empty.")
} else {
    print("The shopping list isn't empty.")
}

//您可以通过调用数组的append(_:)方法将新项添加到数组的末尾:

shoppingList.append("Flour")

//或者,使用加法赋值运算符 ( +=)附加一个或多个兼容项的数组:

// shoppingList now contains 4 items
shoppingList += ["Baking Powder"]

// shoppingList now contains 7 items
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]

//使用下标语法从数组中检索值
//并在数组名称后方括号内立即传递要检索的值的索引:

// firstItem is equal to "Eggs"
var firstItem = shoppingList[0]

//您可以使用下标语法更改给定索引处的现有值:

// the first item in the list is now equal to "Six eggs" rather than "Eggs"
shoppingList[0] = "Six eggs"

//当您使用下标语法时,您指定的索引需要有效。例如,尝试在项目数组的末尾写入新值到会导致运行时错误。

//Fatal error: Index out of range
//shoppingList[shoppingList.count] = "Salt"

//["Six eggs", "Milk", "Flour", "Baking Powder", "Chocolate Spread", "Cheese", "Butter"]
print(shoppingList)

//您还可以使用下标语法一次更改值范围,即使替换值集的长度与您要替换的范围不同

shoppingList[4...6] = ["Bananas", "Apples"]

//["Six eggs", "Milk", "Flour", "Baking Powder", "Bananas", "Apples"]
print(shoppingList)

//要将项目插入到指定索引处的数组中,请调用数组的insert(_:at:)方法:

// shoppingList now contains 7 items
// "Maple Syrup" is now the first item in the list
shoppingList.insert("Maple Syrup", at: 0)

//类似地,您可以使用remove(at:)方法从数组中删除一个项目。
//此方法删除指定索引处的项目并返回删除的项目(如果不需要,可以忽略返回值)

// the item that was at index 0 has just been removed
// shoppingList now contains 6 items, and no Maple Syrup
// the mapleSyrup constant is now equal to the removed "Maple Syrup" string
let mapleSyrup = shoppingList.remove(at: 0)

//备注:
//如果您尝试访问或修改数组现有边界之外的索引值,则会触发运行时错误。
//您可以在使用索引之前,通过将其与数组的count属性进行比较来检查索引是否有效。
//因为数组是从零开始索引的,数组中最大的有效索引是count - 1,当count是0没有有效索引时,意味着数组为空

//如果要从数组中删除最后一项,请使用removeLast()方法而不是remove(at:)方法,以避免需要查询数组的count属性。
//与remove(at:)方法一样,removeLast()返回移除的项目

// the last item in the array has just been removed
// shoppingList now contains 5 items, and no apples
// the apples constant is now equal to the removed "Apples" string
let apples = shoppingList.removeLast()

//7.迭代数组
//可以使用for-in循环,遍历整个集合值与数组

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

//如果您需要每个项目的整数索引及其值,请改用该enumerated()方法迭代数组。
//对于数组中的每个项目,该enumerated()方法返回一个由整数和项目组成的元组。
//整数从零开始,每迭代一项索引值加一;如果枚举整个数组,这些整数与项目的索引匹配作为迭代的一部分,您可以将元组分解为临时常量或变量

// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Bananas
for (index, value) in shoppingList.enumerated() {
    print("Item \(index + 1): \(value)")
}

//三、集合(Sets)
//集合,存储相同类型的不同值,没有定义排序的集合。
//当项目的顺序不重要时,或者当您需要确保一个项目只出现一次时,您可以使用集合而不是数组。

//1.设置类型语法
//Swift 集合的类型写为Set<Element>,其中Element是集合允许存储的类型。
//与数组不同,集合没有等效的简写形式

//2.创建和初始化一个空集
//您可以使用初始化语法创建特定类型的空集

var letters = Set<Character>()

// Prints "letters is of type Set<Character> with 0 items."
print("letters is of type Set<Character> with \(letters.count) items.")

//如果上下文已经提供类型信息,例如函数参数或已经类型化的变量或常量,您可以使用空数组文字创建一个空集:

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>

//3.使用数组创建集合
//您还可以使用数组初始化一个集合

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

//由于Swift的类型推断,还可以更短的形式初始化集合favoriteGenres

var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]

//因为数组中的所有值都是相同的类型,Swift可以推断这Set<String>是用于favoriteGenres变量的正确类型。

//4.访问和修改集合
//可以通过其方法和属性访问和修改集合
//要找出集合中的项目数,可以使用只读count属性:

// Prints "I have 3 favorite music genres."
print("I have \(favoriteGenres.count) favorite music genres.")

//使用isEmpty属性,检查count属性是否等于0

// Prints "I have particular music preferences."
if favoriteGenres.isEmpty {
    print("As far as music goes, I'm not picky.")
} else {
    print("I have particular music preferences.")
}

//可以通过调用集合的insert(_:)方法将新项目添加到集合中:

// favoriteGenres now contains 4 items
favoriteGenres.insert("Jazz")

//可以通过调用Set的remove(_:)方法从集合中删除项目
//如果该项目是集合的成员,则删除该项目,并返回删除的值,如果集合不包含它,则返回nil
//可以使用removeAll()方法删除集合中的所有项目

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

//要检查集合是否包含特定项目,使用contains(_:)方法。

// Prints "It's too funky in here."
if favoriteGenres.contains("Funk") {
    print("I get up on the good foot.")
} else {
    print("It's too funky in here.")
}

//5.迭代一个集合
//可以使用for-in循环遍历集合中的值

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

//Swift中的Set类型没有定义顺序
//要以特定顺序迭代集合的值,请使用sorted()方法
//该方法将集合的元素使用升序的数组返回

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

//6.集合操作
//可以使用集合基本的操作,对集合执行交集、并集、差集、补集操作

//7.集合的基本运算
//交集:对于两个给定集合A、B,由属于A又属于B的所有元素构成的集合,叫做A和B的交集。
//并集:对于两个给定集合A、B,由两个集合所有元素构成的集合,叫做A和B的并集。
//差集:记A,B是两个集合,则所有属于A且不属于B的元素构成的集合,叫做集合A减集合B(或集合A与集合B之差)
//补集:一般地,设S是一个集合,A是S的一个子集,由S中所有不属于A的元素组成的集合,叫做子集A在S中的绝对补集。

//总结:
//交集:A & B,即A与B ( x x ( 😄😄 ) x x )
//并集:A | B, 即A或B ( 😄😄 ( 😄😄 ) 😄😄 )
//差集:A - B, 即A减B ( 😄😄 ( x x ) x x )
//补集:A ^ B,即A异B ( 😄😄 ( x x ) 😄😄 )

//交集:使用集合的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]

// 并集:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.union(evenDigits).sorted()

// 交集:[]
oddDigits.intersection(evenDigits).sorted()

// 差集:[1, 9]
oddDigits.subtracting(singleDigitPrimeNumbers).sorted()

// 补集:[1, 2, 9]
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()

//8.集合的成员和集合的相等性
//使用“等于”运算符 ( ==) 来确定两个集合包含的所有值是否相同
//使用isSubset(of:)方法确定一个集合的所有值是否都包含在指定的集合中。
//使用isSuperset(of:)方法确定一个集合是否包含指定集合中的所有值。
//使用isStrictSubset(of:)和isStrictSuperset(of:)方法来确定一个集合是子集还是超集,两个集合相当除外
//使用isDisjoint(with:)方法确定两个集合是否有共同的值,没有返回true,有返回false

let houseAnimals: Set = ["🐶", "🐱"]
let farmAnimals: Set = ["🐮", "🐔", "🐑", "🐶", "🐱"]
let cityAnimals: Set = ["🐦", "🐭"]

// 集合houseAnimals是集合farmAnimals的子集
houseAnimals.isSubset(of: farmAnimals)

// 集合farmAnimals是集合houseAnimals的超集
farmAnimals.isSuperset(of: houseAnimals)

// 集合farmAnimals和集合cityAnimals没有共同值
farmAnimals.isDisjoint(with: cityAnimals)

//四、字典(Dictionary)
//字典中存储的是key-value关联的无序集合,keys和values分别是同种类型的组数
//每一个值都关联着一个唯一的key,key是value在字典中的标识

//1.字典类型速记语法
//Swift字典的类型全写为Dictionary<Key, Value>
//其中是Key可以用作字典键,Value是字典键存储的值
//缩写形式[Key: Value]

//2.创建一个空字典
//与数组一样,您可以使用Dictionary初始化语法创建特定类型的字典
//下面创建的空字典,Key的存储值类型为Int,Value的存储值类型为String

// namesOfIntegers is an empty [Int: String] dictionary

//var namesOfIntegers: Dictionary<Int, String> = [:]
var namesOfIntegers: [Int: String] = [:]

//为字典赋值

// namesOfIntegers now contains 1 key-value pair
namesOfIntegers[16] = "sixteen"

//字典置空

// namesOfIntegers is once again an empty dictionary of type [Int: String]
namesOfIntegers = [:]

//3、字典的初始化和赋值
//键值对(Key-Value Pair)是一个键和值的组合。
//在有值字典中,每个键值对中的键和值用冒号分隔。
//键值对写成一个列表,用逗号分隔,
//字典值用一对方括号括起来
//[key 1: value 1, key 2: value 2, key 3: value 3]

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

//因为字典的Key-Value键值对,都具有确定的值类型,字典初始化可以缩写为:

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

//4.访问和修改字典
//使用Dictionary的count属性,统计字典成员总数

// Prints "The airports dictionary contains 2 items."
print("The airports dictionary contains \(airports.count) items.")

//使用Dictionary的isEmpty方法,可以快速检查字典的count属性是否为0

// Prints "The airports dictionary isn't empty."
if airports.isEmpty {
    print("The airports dictionary is empty.")
} else {
    print("The airports dictionary isn't empty.")
}

//可以通过下标语法,为字典添加一个新的键值对

// the airports dictionary now contains 3 items
airports["LHR"] = "London"

//可以通过下标语法改变某个Key的值

// the value for "LHR" has been changed to "London Heathrow"
airports["LHR"] = "London Heathrow"

//在执行updateValue(_:forKey:)方法的时候,如果健存在,返回旧值的可选值,如果健不存在,则返回nil

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

//使用下标语法访问字典值的时候,如果健存在,返回旧值的可选值,如果健不存在,则返回nil

// Prints "The name of the airport is Dublin Airport."
if let airportName = airports["DUB"] {
    print("The name of the airport is \(airportName).")
} else {
    print("That airport isn't in the airports dictionary.")
}

//可以使用下标语法,给Key赋值为nil,删除一对Key-Value键值对

// "Apple International" isn't the real airport for APL, so delete it
airports["APL"] = "Apple International"

// APL has now been removed from the dictionary
airports["APL"] = nil

//同样,可以使用removeValue(forKey:)方法删除删除一对Key-Value键值对
//如果Key存在,删除键值对的同时返回删除的值

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

//5.遍历字典
//可以使用for-in循环遍历字典,每次循环返回(key,value)元组
//可以使用一个零时变量或常量接收返回值

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

//也可以通过访问字典的keys和values属性,取到字典的keys和values的迭代数组

// Airport code: LHR
// Airport code: YYZ
for airportCode in airports.keys {
    print("Airport code: \(airportCode)")
}

// Airport name: London Heathrow
// Airport name: Toronto Pearson
for airportName in airports.values {
    print("Airport name: \(airportName)")
}

//如果需要使用字典的keys和values,可以通过字典的keys和values属性初始化一个新数组Array

// airportCodes is ["LHR", "YYZ"]
let airportCodes = [String](airports.keys)

// airportNames is ["London Heathrow", "Toronto Pearson"]
let airportNames = [String](airports.values)

//可以使用字典的sorted()方法,得到keys和values的有序数组
// airportCodes is ["LHR", "YYZ"]
airports.keys.sorted()

// airportNames is ["London Heathrow", "Toronto Pearson"]
airports.values.sorted()

print("every thing is good!!!")
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容