Transforms all elements from the upstream publisher with a provided closure.
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
A closure that takes one element as its parameter and returns a new element.
A publisher that uses the provided closure to map elements from the upstream publisher to new elements that it then publishes.
map(_:) operator performs a function similar to that of
map(_:) in the Swift standard library: it uses a closure to transform each element it receives from the upstream publisher. You use
map(_:) to transform from one kind of element to another.
The following example uses an array of numbers as the source for a collection based publisher. A
map(_:) operator consumes each integer from the publisher and uses a dictionary to transform it from its Arabic numeral to a Roman equivalent, as a
String. If the
map(_:)’s closure fails to look up a Roman numeral, it returns the string
If your closure can throw an error, use Combine’s
try operator instead.