Array中的map和flapMap的一点区别
注意:map和flapMap方法在很多类型里都有定义,一定要看清楚方法声明。
map的声明:1
@inlinable public func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]
它返回的是一个数组.闭包的返回值是一个新数组的元素. 默认情况下,只要闭包有可能返回一个optional类型元素,则map返回的将是一个[T?]数组.
flapMap的声明: 有两种声明
1 | @available(swift, deprecated: 4.1, renamed: "compactMap(_:)", message: "Please use compactMap(_:) for the case where closure returns an optional value") |
二者区别:
flatMap可以用来展平一个数组:它把原序列里的元素变为一个序列,然后把这个序列的元素添加到结果序列中,相当于降维了。展的有多平得看transform的实现,不过最少是减一层。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34let links: [[String]] = [
["http://a1", "http://a2", "http://a3"],
["http://b1", "http://b2", "http://b3"],
["http://c1", "http://c2", "http://c3"],
]
print(links)
let flapLink = links.flatMap { (exlinks: [String]) -> [String] in
return exlinks //因为links的元素就是数组所以这里直接返回就可以了。
}
print(flapLink) //["http://a1", "http://a2", "http://a3", "http://b1", "http://b2", "http://b3", "http://c1", "http://c2", "http://c3"]
let d3: [[[Int]]] = [
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
[[19, 20, 21], [22, 23, 24], [25, 26, 27]],
]
//[[Int]]
let flapD1 = d3.flatMap { ele in
return ele
}
//[Int]
let flapD2 = d3.flatMap { ele in
return ele.flatMap{$0}
}
let colors = ["♥", "♠", "♦", "♣"]
let numbers = ["J", "Q", "K", "A"]
let cards = colors.flatMap { color in
return numbers.map { num in
return "\(color)-\(num)"
}
}
print(cards) //["♥-J", "♥-Q", "♥-K", "♥-A", "♠-J", "♠-Q", "♠-K", "♠-A", "♦-J", "♦-Q", "♦-K", "♦-A", "♣-J", "♣-Q", "♣-K", "♣-A"]flatMap可以将转化失败的nil元素剔除,而map则不能:
1
2
3
4
5
6
7
8
9
10
11
12let wq = ["123", "fsd", "dfsd", "34"]
//类型是[Int?]
let wqm = wq.map { (element) -> Int? in
return Int(element)
}
print(wqm) //[Optional(123), nil, nil, Optional(34)]
//类型是[Int]
let wqfm = wq.flatMap { (element) -> Int? in
return Int(element)
}
print(wqfm) //[123, 34]
由于剔除nil元素的用法已经被废弃,Apple推荐使用compactMap代替,因此flatMap的用法主要是第一种了.
Optional枚举类型中的map和flatMap
map:
1 | /// Evaluates the given closure when this `Optional` instance is not `nil`, |
flatMap:
1 | /// Evaluates the given closure when this `Optional` instance is not `nil`, |
上面两个区别仅是闭包参数的返回值不同。不仔细看还真发现不了。如果transform的结果可能为nil则需要使用flatMap。如果不可能为nil则可以使用map。