Combine Basics
Combine is Apple's toolkit for handling values that arrive over time, like a stream of updates flowing through pipes you connect together.
Syntax
import Combine
let cancellable = Just(value)
.map { $0 transformation }
.sink { received in
// use received
}
A Minimal Publisher and Subscriber
A Just publisher wraps a single value and immediately delivers it to any subscriber attached with .sink, making it the simplest possible Combine pipeline to demonstrate deterministically.
Note:
- Conceptual note: this is real, syntactically correct Combine code as it would run on an Apple platform (macOS/iOS) with Xcode.
- Combine is an Apple-platform framework and its module is not available in open-source Swift toolchains on Linux, so this specific example cannot be executed in this course's Linux-based sandbox -- treat it as a conceptual reference rather than a runnable one here.
Warning:
Combine requires an Apple platform (macOS/iOS with Xcode) and does not run in this course's Linux-based code sandbox.
Example: A Minimal Publisher and Subscriber
import Combine
var subscriptions = Set<AnyCancellable>()
let publisher = Just("Hello from Combine")
publisher.sink { value in
print(value)
}.store(in: &subscriptions)
Login to try C/C++/Java/PHP code in the editor
Transforming Values with map
Combine publishers support operators like .map to transform emitted values before they reach the subscriber, similar to Array.map.
Warning:
Combine requires an Apple platform (macOS/iOS with Xcode) and does not run in this course's Linux-based code sandbox.
Example: Transforming Values with map
import Combine
var subscriptions = Set<AnyCancellable>()
let publisher = Just(5)
publisher.map { $0 * $0 }.sink { value in
print("Squared: \(value)")
}.store(in: &subscriptions)
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Assuming Combine values from a
Justpublisher arrive asynchronously across multiple run-loop turns; aJustpublisher actually emits its single value synchronously to the subscriber. - Forgetting to keep a strong reference (e.g. in a
Set<AnyCancellable>) to a subscription; letting it deallocate immediately can stop the pipeline from delivering values. - Using Combine for a one-off synchronous computation where a plain function would be simpler; Combine shines for ongoing streams of values over time.
Chapter Summary
- Combine models asynchronous event streams using
Publishers andSubscribers. .sink { }is a simple way to subscribe to a publisher's values directly with a closure.- A
Justpublisher emits exactly one value and then finishes immediately. - Real Combine pipelines commonly involve network calls or timers, which need a running event loop to observe -- this example intentionally uses the simplest possible synchronous publisher so its output is deterministic.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: