What is the ObservableObject Protocol in Swift’s Combine?

Allow your object to self-monitor its properties

Zafar Ivaev
Clean Software

--

Photo by Wesson Wang on Unsplash

Conforming to the ObservableObject protocol provides a handy way to monitor when one of its @Published properties change. However, we can’t know which exact property has changed. It is created to help you when using SwiftUI in your projects.

Without a further ado, let’s explore the ObservableObject protocol in practice.

Let’s Start

Say we have a simple class called ObjectToBeObserved:

What if we want to be notified when its value property is changed? We can conform to the ObservableObject and mark the value property with a Published property wrapper:

--

--

Zafar Ivaev
Clean Software

Senior iOS Engineer. I write about features of Swift and iOS development practices.