flatMap: 對數(shù)組的每一個(gè)元素做一次處理盗痒,返回處理后的數(shù)組。
與map的區(qū)別是:
- 返回后的數(shù)組中不存在nil低散, 同時(shí)也會把Optional解包俯邓。
函數(shù)聲明:
@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]
詳解:
我們并沒有強(qiáng)制解包,但是返回元素已不是可選類型引镊,而且沒有nil朦蕴。
// 簡化寫法
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"]
//需求一:計(jì)算每個(gè)元素的長度
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"]
上面的例子中,我們雖然返回了nil弟头,但是結(jié)果中吩抓,卻是沒有nil,也不是可選類型赴恨。
2.flatMap還能把多維數(shù)組變成一維數(shù)組疹娶。
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)")
/*
結(jié)果:
arr1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
- flatMap也能把兩個(gè)不同的數(shù)組合并成一個(gè)數(shù)組,這個(gè)合并的數(shù)組元素個(gè)數(shù)是前面兩個(gè)數(shù)組元素
個(gè)數(shù)的乘積伦连。
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)")
/*
返回結(jié)果:
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"]
*/
compactMap與flatMap的區(qū)別上面也說了雨饺,當(dāng)閉包中的返回結(jié)果是可選的時(shí)候,使用compactMap代替flatMap惑淳,那么當(dāng)閉包中的返回結(jié)果不是可選的時(shí)候额港,依然使用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"]
結(jié)果可以看出汛聚,雖然閉包返回值是可選的锹安,但是真正返回的結(jié)果中短荐,并不是可選的倚舀,也會過濾掉nil的情況叹哭。