flatMap is now (partly) compactMap()
About 1 min
flatMap is now (partly) compactMap() 관련
HACKING WITH SWIFT
What's new in Swift?
flatMap is now (partly) compactMap() | Changes in Swift 4.1
flatMap is now (partly) compactMap()
Available from Swift 4.1
The flatMap()
method was useful for a variety of things in Swift 4.0, but one was particularly useful: the ability to transform each object in a collection, then remove any items that were nil.
Swift Evolution proposal SE-0187 (apple/swift-evolution
) suggested changing this, and as of Swift 4.1 this flatMap()
variant has been renamed to compactMap()
to make its meaning clearer.
For example:
let array = ["1", "2", "Fish"]
let numbers = array.compactMap { Int($0) }
That will create an Int
array containing the numbers 1 and 2, because "Fish" will fail conversion to Int
, return nil, and be ignored.
Other Changes in Swift 4.1
Synthesized Equatable and Hashable | Changes in Swift 4.1
Synthesized Equatable and Hashable
Key decoding strategies for Codable | Changes in Swift 4.1
Key decoding strategies for Codable
Conditional conformances | Changes in Swift 4.1
Conditional conformances
Recursive constraints on associated types | Changes in Swift 4.1
Recursive constraints on associated types
Build configuration import testing | Changes in Swift 4.1
Build configuration import testing
Target environment testing | Changes in Swift 4.1
Target environment testing