Member-only story

What is Swift’s @ObservedObject Property Wrapper?

A way of monitoring state

Steven Curtis
3 min readNov 8, 2024
Photo by David Clode on Unsplash

I’ve got to admit it. I’ve been using the ObservedObject property wrapper to hold a view model in a view for the longest time, without really considering what it does or why.

So I’ve created this article as a breakdown of how ObservedObject works, how to use it effectively and some best practices.

What is @ObservedObject?

ObservedObject is a property wrapper that marks a property in a view that references an observable object, conforming to the ObservableObject protocol. This allows the view to monitor the object conforming to ObservableObject for any changes, so the view knows when to update itself when the observed object’s properties change.

This is commonly used when the backend updates should trigger UI changes.

Using @ObservedObject

A view can hold onto the view model, so this can be accessed from within the view.

struct ContentView: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
Text("\(viewModel.error?.localizedDescription)")
.onAppear {
viewModel.throwError()
}
}
}

--

--

No responses yet