← Back to Swift Course | Chapter 14: Standard Library & Best Practices | Lesson 6 of 7

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.

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

markup
import Combine
var subscriptions = Set<AnyCancellable>()
let publisher = Just("Hello from Combine")
publisher.sink { value in
    print(value)
}.store(in: &subscriptions)

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

markup
import Combine
var subscriptions = Set<AnyCancellable>()
let publisher = Just(5)
publisher.map { $0 * $0 }.sink { value in
    print("Squared: \(value)")
}.store(in: &subscriptions)
Common Mistakes
  1. Assuming Combine values from a Just publisher arrive asynchronously across multiple run-loop turns; a Just publisher actually emits its single value synchronously to the subscriber.
  2. 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.
  3. 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 and Subscribers.
  • .sink { } is a simple way to subscribe to a publisher's values directly with a closure.
  • A Just publisher 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.