@StateObject, @EnvironmentObject, and @ObservedObject
I’ve decided to dedicate this week to exploring data flow in SwiftUI. In this article, we’ll discuss the differences between the @StateObject, @EnvironmentObject, and @ObservedObject property wrappers. From my experience, this is often the most confusing topic for developers just starting out with SwiftUI.
Why do we need property wrappers in SwiftUI?
SwiftUI uses immutable struct types to describe the view hierarchy. Every view provided by the framework is inherently immutable. This is why SwiftUI provides a specific set of property wrappers to handle data changes.