# swift, Array的高阶函数：flatMap 和 compactMap

image.png

flatMap: 对数组的每一个元素做一次处理，返回处理后的数组。

1. 返回后的数组中不存在nil， 同时也会把Optional解包。
函数声明：
@available(swift, deprecated: 4.1, renamed: "compactMap(:)", message: "Please use compactMap(:) for the case where closure returns an optional value")
`public func flatMap(_ transform: (Element) throws -> String?) rethrows -> [String]`
注意：4.1之后如果闭包中返回的值是可选的话，就要使用compactMap代替flatMap了，不然的话就会警告。
``````        let arrayAny: [Any?] = [1, 2, 3, 4, 5, nil, "a", 8, "9"]
let arrayInt = arrayAny.flatMap { (obj) -> Any? in
if obj is Int {
return obj
} else {
return nil
}
}
print("arrayInt: \(arrayInt)")
// arrayInt: [1, 2, 3, 4, 5, 8]
``````

``````       // 简化写法
let arrayInt2 = arrayAny.flatMap { \$0 is Int ? \$0 : nil }
print("arrayInt2: \(arrayInt2)")
// arrayInt2: [1, 2, 3, 4, 5, 8]
``````
`````` print("-----------------案例二----------------------")
let arrayString = ["Ann", "Bob", "Tom", "Lily", "HanMeiMei", "Jerry"]
//需求一：计算每个元素的长度
let arrayCount = arrayString.flatMap { (str) -> Int in
return str.count
}
print("arrayCount: \(arrayCount)")
// arrayCount: [3, 3, 3, 4, 9, 5]

// 简化
let arrayCount2 = arrayString.flatMap { \$0.count }
print("arrayCount2: \(arrayCount2)")
// arrayCount2: [3, 3, 3, 4, 9, 5]
``````
``````        print("-----------------案例三----------------------")
let arrayThree = ["Apple", "Orange", "Puple", ""]
let arrayT = arrayThree.flatMap { (str) -> String? in
return str.count > 0 ? str : nil
}
print("arrayT: \(arrayT)")
// arrayT: ["Apple", "Orange", "Puple"]

// 简化
let arrayT2 = arrayThree.flatMap { \$0.count > 0 ? \$0 : nil }
print("arrayT2: \(arrayT2)")
// arrayT2: ["Apple", "Orange", "Puple"]
``````

2.flatMap还能把多维数组变成一维数组。

``````        print("-----------------案例四----------------------")
let arrayA = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let arr1 = arrayA.map { \$0 }
let arr2 = arrayA.flatMap { \$0 }
print("arr1 = \(arr1)")
print("arr2 = \(arr2)")
/*
结果：
arr1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
``````
1. flatMap也能把两个不同的数组合并成一个数组，这个合并的数组元素个数是前面两个数组元素
个数的乘积。
``````        print("-----------------案例五----------------------")
let fruits = ["Apple", "Orange", "Puple", "Pear"]
let counts = [2, 3, 6, 1]
let arrayMerge = counts.flatMap { (count) -> [String] in
fruits.map({ return \$0 + ": \(count)"})
}
print("arrayMerge = \(arrayMerge)")
/*
返回结果：
arrayMerge = ["Apple: 2", "Orange: 2", "Puple: 2", "Pear: 2", "Apple: 3", "Orange: 3", "Puple: 3", "Pear: 3", "Apple: 6", "Orange: 6", "Puple: 6", "Pear: 6", "Apple: 1", "Orange: 1", "Puple: 1", "Pear: 1"]
*/

``````
image.png

compactMap与flatMap的区别上面也说了，当闭包中的返回结果是可选的时候，使用compactMap代替flatMap，那么当闭包中的返回结果不是可选的时候，依然使用flatMap。

``````let arrayString = ["Ann", "Bob", "Tom", "Lily", "HanMeiMei", "Jerry"]
let arrayInt = arrayString.compactMap { (str) -> Int? in
return str.count
}
print("arrayInt: \(arrayInt)")
// arrayInt: [3, 3, 3, 4, 9, 5]

// 简化
let arrayInt2 = arrayString.compactMap { \$0.count }
print("arrayInt2: \(arrayInt2)")
// arrayInt2: [3, 3, 3, 4, 9, 5]

let arrayI = arrayString.compactMap { \$0.contains("i") ? \$0 : nil }
print("arrayI: \(arrayI)")
// arrayI: ["Lily", "HanMeiMei"]
``````

swift学习