Swift集合类型高阶函数(一)

Map(swift 5.3)的使用

对集合类型中的每一个元素做一次处理,转换为新数组

数组系列

  • 案例1 - 遍历每个元素
1
2
3
4
5
6
let colors = ["red", "yellow", "green", "blue"]
let counts = colors.map { (color: String) -> Int in
return color.count
}

print(counts)

结果是 [3,6,5,4]

  • 案例2 - 更加简单的方法
1
2
let counts1 = colors.map { $0.count }
print(counts1)

结果也是 [3,6,5,4]

  • 案例3 - 转换为对象数组(请问下转换为对象数组干啥用)
1
2
3
4
5
6
7
8
9
10
11
12
class Color {
var name: String
init(name: String) {
self.name = name
}
}

let colorsObj = colors.map { return Color(name: $0) }

for obj in colorsObj {
print(obj.name)
}

结果是

1
2
3
4
red
yellow
green
blue

集合系列

1
2
3
let ColorsSet: Set = ["red", "yellow", "green", "blue"]
let colorsCount = ColorsSet.map { $0.count }
print(colorsCount)

结果是**[3, 6, 4, 5]**

字典系列

1
2
3
4
5
let dict = [2: "red", 4: "yellow", 6: "green", 8: "blue"]
let keys = dict.map { $0.key }
print(keys)
let values = dict.map { $0.value }
print(values)

结果分别是

[2, 8, 6, 4]

[“red”, “blue”, “green”, “yellow”]