Logo for tanaschita.com

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.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

allSatisfy(_:)

Publishes true if all elements match the condition. Publishes false & completes if it receives an element that does not match the condition.

Combine operator

append(_:)

Appends the specified elements after the upstream publisher completes.

Combine operator

collect(_:)

Collects up to the specified number of elements and emits them in an array.

Combine operator

combineLatest(_:)

Publishes a tuple of the most recent element from multiple publishers when any of them emits a value.

Combine operator

compactMap(_:)

Transforms each element with the provided closure and publishes any returned non-nil optional result.

Combine operator

contains(_:)

Publishes true & completes after receiving a matching value, or false when upstream publisher completes without producing a matching value.

Combine operator

count()

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

Combine operator

dropFirst(_:)

Omits a specified number of elements, then republishes all remaining elements.

Combine operator

drop(while:)

Omits elements until a given closure returns false, then republishes all remaining elements.

Combine operator

filter(_:)

Republishes all elements that match a provided closure.

Combine operator

first()

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

Combine operator

first(where:)

Publishes the first element of a stream that satisfies a condition, then completes.

Combine operator

flatMap(maxPublishers:_:)

Transforms all elements from an upstream publisher into a new publisher.

Combine operator

ignoreOutput()

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

Combine operator

last()

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

Combine operator

last(where:)

Publishes the last element that satisfies a condition after the upstream publisher completes.

Combine operator

map(_:)

Transforms each element it receives from the upstream publisher with the provided closure.

Combine operator

max()

Publishes the maximum value received after the upstream publisher completes.

Combine operator

merge(with:)

Publishes an element whenever any of the upstream publishers emits an element.

Combine operator

min()

Publishes the minimum value received after the upstream publisher completes.

Combine operator

output(at:)

Republishes an element specified by its position, or completes if upstream publisher completes before the specified element.

Combine operator

output(in:)

Republishes elements specified by a range of positions.

Combine operator

prefix(_:)

Republishes elements up to a specified maximum count.

Combine operator

prefix(while:)

Emits values while elements meet the specified condition, otherwise it completes.

Combine operator

prepend(_:)

Prepends elements with the specified values.

Combine operator

reduce(_:_:)

Applies a closure to all elements and publishes a final result when the upstream publisher completes.

Combine operator

removeDuplicates()

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

Combine operator

replaceNil(with:)

Replaces nil elements with the provided element.

Combine operator

scan(_:_:)

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

Combine operator

switchToLatest()

Republishes elements sent by the most recently received publisher.

Combine operator

zip(_:)

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

Combine operator
Download cheat sheet as pdf

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

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

combine

reactive programming

swift

Memory management in Combine

Learn how to avoid retain cycles when using the Combine framework in Swift.

12 Sep 2022 · 4 min read

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum