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.
Publishes true if all elements match the condition. Publishes false & completes if it receives an element that does not match the condition.
Appends the specified elements after the upstream publisher completes.
Collects up to the specified number of elements and emits them in an array.
Publishes a tuple of the most recent element from multiple publishers when any of them emits a value.
Transforms each element with the provided closure and publishes any returned non-nil optional result.
Publishes true & completes after receiving a matching value, or false when upstream publisher completes without producing a matching value.
Publishes the count of all received elements when the upstream publisher completes.
Omits a specified number of elements, then republishes all remaining elements.
Omits elements until a given closure returns false, then republishes all remaining elements.
Republishes all elements that match a provided closure.
Publishes the first element of the upstream publisher, then completes.
Publishes the first element of a stream that satisfies a condition, then completes.
Transforms all elements from an upstream publisher into a new publisher.
Ignores all elements and passes along only the upstream publisher's completion state.
Publishes the last element of the upstream publisher after it completes.
Publishes the last element that satisfies a condition after the upstream publisher completes.
Transforms each element it receives from the upstream publisher with the provided closure.
Publishes the maximum value received after the upstream publisher completes.
Publishes an element whenever any of the upstream publishers emits an element.
Publishes the minimum value received after the upstream publisher completes.
Republishes an element specified by its position, or completes if upstream publisher completes before the specified element.
Republishes elements specified by a range of positions.
Republishes elements up to a specified maximum count.
Emits values while elements meet the specified condition, otherwise it completes.
Prepends elements with the specified values.
Applies a closure to all elements and publishes a final result when the upstream publisher completes.
Publishes an element only if it doesn't match the previous element.
Replaces nil elements with the provided element.
Transforms elements by applying a closure that receives its previous return value and the next element from the upstream publisher.
Republishes elements sent by the most recently received publisher.
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.
Like to support my work?
Articles with related topics
Memory management in Combine
Learn how to avoid retain cycles when using the Combine framework in Swift.
12 Sep 2022 · 4 min read
How to bridge async/await functions to Combine's Future type in Swift
Learn how to call async/await code within Combine based APIs.
22 Aug 2022 · 2 min read
Quick guide on Combine essentials
Get started with Combine by reading this guide on basic Combine concepts and terms.
15 Nov 2020 · 10 min read
Introduction to Combine for iOS developers with RxSwift experience
How to start using Combine for reactive programming
19 Aug 2019 · 4 min read
How to use a Connectable publisher in Combine
Learn how to avoid data loss by using Combine's ConnectablePublisher.
13 Dec 2021 · 2 min read
Latest articles and tips
Introduction to Kotlin for Swift developers - protocols, extensions & generics
Learn how protocols, extensions and generics in Kotlin work from a Swift developer's point of view.
02 Dec 2023 · 8 min read
How to store images in SwiftData
Learn how to efficiently store larger amounts of data in SwiftData.
27 Nov 2023 · 1 min read
How to migrate to a new schema with SwiftData in iOS
Learn how to create a migration plan in SwiftData.
20 Nov 2023 · 4 min read
How to implemet a free trial period for StoreKit 2 subscriptions in iOS
Learn how to implement StoreKit 2 introductory offers.
13 Nov 2023 · 2 min read
How to migrate from Localizable.strings to String Catalogs
Learn how to migrate your iOS project to .xcstrings for localization.
06 Nov 2023 · 1 min read