---
title: "map(_:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/map(_:)-99evh"
---

# map(_:)

Transforms all elements from the upstream publisher with a provided closure.

## Declaration

```swift
func map<T>(_ transform: @escaping (Self.Output) -> T) -> Publishers.Map<Self, T>
```

## Parameters

- `transform`: A closure that takes one element as its parameter and returns a new element.

## Mentioned in

Receiving and Handling Events with Combine

## Return Value

Return Value A publisher that uses the provided closure to map elements from the upstream publisher to new elements that it then publishes.

## Discussion

Discussion Combine’s 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 (unknown). let numbers = [5, 4, 3, 2, 1, 0] let romanNumeralDict: [Int : String] =    [1:"I", 2:"II", 3:"III", 4:"IV", 5:"V"] cancellable = numbers.publisher     .map { romanNumeralDict[$0] ?? "(unknown)" }     .sink { print("\($0)", terminator: " ") }

// Prints: "V IV III II I (unknown)" If your closure can throw an error, use Combine’s tryMap(_:) operator instead.

## See Also

### Mapping elements

- [tryMap(_:)](combine/publisher/trymap(_:).md)
- [mapError(_:)](combine/publisher/maperror(_:).md)
- [replaceNil(with:)](combine/publisher/replacenil(with:).md)
- [scan(_:_:)](combine/publisher/scan(_:_:).md)
- [tryScan(_:_:)](combine/publisher/tryscan(_:_:).md)
- [setFailureType(to:)](combine/publisher/setfailuretype(to:).md)
