[Swift]遍历集合类型(数组、集合和字典)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤:⼭青咏芝(shanqingyongzhi)
➤博客园地址:⼭青咏芝()
➤GitHub地址:
➤原⽂地址:
➤如果链接不是⼭青咏芝的博客园地址,则可能是爬取作者的⽂章。
➤原⽂已修改更新!强烈建议点击原⽂地址阅读!⽀持作者!⽀持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Swift提供了三种主要的集合类型,称为数组,集合和字典,⽤于存储值集合。
数组是有序的值集合。
集是唯⼀值的⽆序集合。
字典是键值关联的⽆序集合。
Swift中⽆法再使⽤传统形式的for循环。
遍历数组:可以使⽤for - in 循环遍历数组中的值。
如果不需要序列中的每个值,则可以使⽤下划线(_)代替变量名来忽略这些值。
1 //传统for 循环形式不适⽤于Swift
2 for (单次表达式;条件表达式;末尾循环体){中间循环体;}
1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"]
2
3 //⽅法1:根据元素顺序遍历
4 for  item in  arrayStr {
5    print(item)
6 }
7
8 //⽅法2:根据部分元素顺序遍历
9 for  item in  5] {
10    print(item)
11 }
12
13 //⽅法3:根据索引顺序遍历
14 for  index in  0...(unt - 1) {
15    print(arrayStr[index])
16 }
17
18 //⽅法4:初始化数组并顺序遍历
19 for  item in  10) {
20    print(item)
21 }
22
23 //⽅法5:stride 函数
24 //from ,to ,by 最后⼀个值是 >(或<) to 的值
25 //from ,through ,by 最后⼀个值是 >=(或<=) through 的值
26 for  index in  stride(from : 0, to: unt,by: 1 )
27 {
28    print(arrayStr[index])
29 }
1 let base  = 3
2 let power = 10
3var answer = 1
4for _ power {
5    answer *= base
6}
7 print("\(base) to the power of \(power) is \(answer)")
8// Prints "3 to the power of 10 is 59049"
遍历集合:可以使⽤for- in循环遍历集合中的值。
1  let arrayStr:Set<String> = ["s","t","r","e","n","g"]
2//输出的顺序是随机的
3for item in arrayStr {
4    print("\(item)")
5 }
Swift的Set类型没有定义的顺序。要以特定顺序迭代集合的值,请使⽤该sorted()⽅法,该⽅法将集合的元素作为使⽤<;运算符排序的数组返回。
1  let arrayStr:Set<String> = ["s","t","r","e","n","g"]
2//按照字母排序顺序输出
3for item in arrayStr.sorted() {
4    print("\(item)")
5 }
遍历字典(可单独遍历键、值、或键值)
可以⽤字典遍历键值对for- in环。字典中的每个项都作为元组返回,您可以将元组的成员分解为临时常量或变量,作为迭代的⼀部分:(key, value),还可以通过访问字典的键keys和values属性来检索字典的键或值的可迭代集合:
1var direc: [Int: String] =  [0:"s",1:"t",2:"r",3:"e",4:"n",5:"g"]
2
3//按键值对遍历顺序遍历
4for (num, char) in direc.sorted(by: <) {
5    print("\(num): \(char)")
6}
7
8//按键遍历顺序遍历
9for num in direc.keys.sorted(by: <) {
10    print("\(num)")
11}
12
13//按值遍历顺序遍历
14for char in direc.values {
15    print("\(char)")
16 }
如果您需要使⽤带有Array实例的API的字典键或值,请使⽤keys或values属性初始化新数组:
1 let nums = [Int](direc.keys)
2print(nums)
3//次序随机 [4, 5, 2, 0, 1, 3]
4
5 let chars = [String](direc.values)
6print(chars)
7//次序随机 ["n", "g", "r", "s", "t", "e"]
Swift的Dictionary类型没有定义的顺序。要按特定顺序迭代字典的键或值,请sorted()在其keys或values属性上使⽤该⽅法。
倒序遍历 :reversed() 或 stride函数
Swift中标准库中⽅法
1//可以⽤来快速的枚举数组中的内容,并⽤元组(index, value)接收
2public func enumerated() -> EnumeratedSequence<Array<Element>>
3
4//可以倒叙数组内容
5public func reversed() -> ReversedRandomAccessCollection<Array<Element>>
当两者结合使⽤⽤到索引时需要注意
1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"]
2
3//索引元素值绑定⼀起倒序遍历
4for i umerated().reversed() {
5  print("\(i) ", terminator: "")
6}
7/*
8(offset: 10, element: "!") (offset: 9, element: "n") (offset: 8, element: "e") (offset: 7, element: "h") (offset: 6, element: "t") (offset: 5, element: "g") (offset: 4, element: "n") (offset: 3, element: "e") (offset: 2, element: "r") (offset: 1, element: "t") (offset: 0, element: "s")
9*/
10
11//只元素值倒序遍历,原索引不变
12for i versed().enumerated() {
13  print("\(i) ", terminator: "")
14}
15/*
16(offset: 0, element: "!") (offset: 1, element: "n") (offset: 2, element: "e") (offset: 3, element: "h") (offset: 4, element: "t") (offset: 5, element: "g") (offset: 6, element: "n") (offset: 7, element: "e") (offset: 8, element: "r") (offset: 9, element: "t") (offset: 10, element: "s")
17*/
倒序遍历数组
1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"]
2
3//⽅法1:根据元素倒序遍历
4for item versed() {
5    print(item)
6}
7
8//⽅法2:根据部分元素倒序遍历
9for item in 5].reversed() {
10    print(item)
11}
12
13//⽅法3:根据索引倒序遍历
14for index in (0...(unt - 1)).reversed() {
15    print(arrayStr[index])
16}
17
18//⽅法4:初始化数组并倒序遍历
19for item in 10).reversed() {
20    print(item)
21}
22
23//⽅法4:初始化数组并倒序遍历
24for item in 10).reversed() {
25    print(item)
26}
27
28//⽅法5:stride函数
29//from,to,by 最后⼀个值是 >(或<) to的值
30//from,through,by 最后⼀个值是 >=(或<=) through的值
31for index in stride(from: unt - 1, through: 0,by: -1 )
32{
33    print(arrayStr[index])
34 }
倒序遍历集合
1 let arrayStr:Set<String> = ["s","t","r","e","n","g"]
2//按照字母排序倒序输出
3for item in arrayStr.sorted().reversed() {
4    print("\(item) ", terminator: "")
5}
6//Print "t s r n g e"swift 字符串转数组
倒序遍历字典(可单独遍历键、值、或键值)
1var direc: [Int: String] =  [0:"s",1:"t",2:"r",3:"e",4:"n",5:"g"]
2//按键值对倒序遍历
3for (num, char) in direc.sorted(by: <).reversed() {
4    print("\(num): \(char)")
5 }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。