Combine Getting Started Guide
Learn Swift’s Combine framework through examples and sample code. A brief intro to publishers, operators, and subscribers.
6 min readMar 28, 2021
What is Combine?
In short, Combine enables subscribing to output or values over time with a declarative API. For example, if you’re making a network request to fetch JSON.
Notice how Combine is built into url session, you’ll see it throughout Apple’s API ecosystem i.e notification center, timers.
- Create a new data task publisher to fetch data at a URL.
- Map on the response and get the
Data
via keypaths. - Decode the response into a dictionary.
- CompactMap on the dictionary to the property results.
- Replace errors with an empty array (throwing away the error).
- Create a new publisher on the main queue.
- Assign the subscription input to the text property on
UILable
.