Combine example with URLSession and assign value to a labels text

Combine Getting Started Guide

Learn Swift’s Combine framework through examples and sample code. A brief intro to publishers, operators, and subscribers.

Gary Tokman
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.

  1. Create a new data task publisher to fetch data at a URL.
  2. Map on the response and get the Data via keypaths.
  3. Decode the response into a dictionary.
  4. CompactMap on the dictionary to the property results.
  5. Replace errors with an empty array (throwing away the error).
  6. Create a new publisher on the main queue.
  7. Assign the subscription input to the text property on UILable.