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
How to use the @available attribute in Swift
Learn to make Swift code only available for certain language versions or platforms.
06 Feb 2023 · 2 min read
How to import Objective-C types as optionals or non-optionals into Swift
Learn how to use nullability declarations and macros.
30 Jan 2023 · 3 min read
Developer guide on keychain for iOS
Learn how to securely store passwords with Apple's Keychain API.
23 Jan 2023 · 4 min read
Understanding basic animations in SwiftUI
Learn different options to animate SwiftUI views.
16 Jan 2023 · 4 min read
How to store a Swift struct in UserDefaults
Learn about the pitfalls when using Codable types in UserDefaults.
09 Jan 2023 · 4 min read