MVVM with Combine Tutorial for iOS

MVVM with Combine Tutorial for iOS

Apple’s newest framework Combine, alongside SwiftUI, took WWDC by storm. Combine is a framework which provides logical streams of data which can emit values and then optionally end in a success or error. These streams are at the core of Functional Reactive Programming (FRP) which has become popular over recent years. It has become clear that Apple is moving forward, not only with a declarative way of creating interfaces with SwiftUI, but also with Combine to manage state over time. In this MVVM with Combine tutorial, you’ll create a weather app that takes advantage of SwiftUI, Combine and MVVM as the architectural pattern. By the end of it, you’ll be comfortable with:

[…]

Data Binding is what allows you to connect a View to its ViewModel. Before this year’s WWDC, you would have to use something akin to RxSwift (via RxCocoa) or ReactiveSwift (via ReactiveCocoa). In this tutorial, you’ll explore how you can achieve this connection using SwiftUI and Combine.

[…]

Now open SceneDelegate.swift and replace the old weeklyView property with the following:

[…]

Open WeeklyWeatherViewModel.swift and replace your current initializer with the following:

This code is crucial because it bridges both worlds: SwiftUI and Combine.

[…]

Open WeeklyWeatherViewModel.swift and start using the builder by adding the following at the bottom of the file:

[…]