Cheat sheet on Combine operators for iOS development
Get an overview on Publisher operators in Combine.
21 Nov 2022 · 5 min read
In Combine, operators are methods that perform an operation on values coming from a publisher. The framework provides various built-in operators known from reactive programming.
This guide summerizes available Combine operators providing a diagram for each of them.

allSatisfy(_:)
Publishes true if all elements match the condition. Publishes false & completes if it receives an element that does not match the condition.

append(_:)
Appends the specified elements after the upstream publisher completes.

collect(_:)
Collects up to the specified number of elements and emits them in an array.

combineLatest(_:)
Publishes a tuple of the most recent element from multiple publishers when any of them emits a value.

compactMap(_:)
Transforms each element with the provided closure and publishes any returned non-nil optional result.

contains(_:)
Publishes true & completes after receiving a matching value, or false when upstream publisher completes without producing a matching value.

count()
Publishes the count of all received elements when the upstream publisher completes.

dropFirst(_:)
Omits a specified number of elements, then republishes all remaining elements.

drop(while:)
Omits elements until a given closure returns false, then republishes all remaining elements.

filter(_:)
Republishes all elements that match a provided closure.

first()
Publishes the first element of the upstream publisher, then completes.

first(where:)
Publishes the first element of a stream that satisfies a condition, then completes.

flatMap(maxPublishers:_:)
Transforms all elements from an upstream publisher into a new publisher.

ignoreOutput()
Ignores all elements and passes along only the upstream publisher's completion state.

last()
Publishes the last element of the upstream publisher after it completes.

last(where:)
Publishes the last element that satisfies a condition after the upstream publisher completes.

map(_:)
Transforms each element it receives from the upstream publisher with the provided closure.

max()
Publishes the maximum value received after the upstream publisher completes.

merge(with:)
Publishes an element whenever any of the upstream publishers emits an element.

min()
Publishes the minimum value received after the upstream publisher completes.

output(at:)
Republishes an element specified by its position, or completes if upstream publisher completes before the specified element.

output(in:)
Republishes elements specified by a range of positions.

prefix(_:)
Republishes elements up to a specified maximum count.

prefix(while:)
Emits values while elements meet the specified condition, otherwise it completes.

prepend(_:)
Prepends elements with the specified values.

reduce(_:_:)
Applies a closure to all elements and publishes a final result when the upstream publisher completes.

removeDuplicates()
Publishes an element only if it doesn't match the previous element.

replaceNil(with:)
Replaces nil elements with the provided element.

scan(_:_:)
Transforms elements by applying a closure that receives its previous return value and the next element from the upstream publisher.

switchToLatest()
Republishes elements sent by the most recently received publisher.

zip(_:)
Waits until both publishers emit an element, then publishes the oldest unconsumed element from each one as a tuple.

Note: I build this cheat sheet with SwiftUI. If you are interested in the source code, check out this repository on github.

Newsletter
Like to support my work?
Say hi
Related tags
Articles with related topics
Latest articles and tips