DEV Community

Tech Stack
Tech Stack

Posted on Originally published at techstackgame.com

@State vs @StateObject vs @ObservedObject: The Definitive SwiftUI Comparison

SwiftUI gives you several property wrappers for holding state, and picking the wrong one is one of the most common sources of confusing bugs for developers new to the framework - a view that doesn't update, or a value that resets unexpectedly. Here's what each one actually does.

Quick Comparison

Wrapper Holds Who creates it Minimum iOS
@State Value types (String, Int, Bool, struct) This view iOS 13+
@StateObject A class conforming to ObservableObject This view (created once) iOS 14+
@ObservedObject A class conforming to ObservableObject Passed in from a parent iOS 13+

@State: Local, View-Owned Value Types

@State is for simple value-type state (a String, Int, Bool, or a struct) that belongs to one view and doesn't need to be shared. SwiftUI stores the value outside the view struct itself, so it survives the view being redrawn.

struct CounterView: View {
    @State private var count = 0

    var body: some View {
        Button("Count: \(count)") {
            count += 1
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

@State properties should almost always be marked private - the whole point is that this state is owned by this view alone.

@StateObject: Owning a Reference-Type Model

@StateObject is for when a view needs to create and own an instance of a class conforming to ObservableObject. SwiftUI guarantees it creates the object exactly once for the lifetime of the view, even if the view is recreated due to a parent redrawing.

class UserViewModel: ObservableObject {
    @Published var name = ""
}

struct ProfileView: View {
    @StateObject private var viewModel = UserViewModel()

    var body: some View {
        TextField("Name", text: $viewModel.name)
    }
}
Enter fullscreen mode Exit fullscreen mode

@ObservedObject: Receiving a Model From Outside

@ObservedObject also watches an ObservableObject for changes, but it does not take ownership - it's for when the object is created elsewhere and passed in. Using @ObservedObject for an object the view itself creates is the classic mistake: if the parent view redraws, a new instance gets created and all state on it is lost.

struct ProfileDetailView: View {
    @ObservedObject var viewModel: UserViewModel // passed in from a parent

    var body: some View {
        Text(viewModel.name)
    }
}
Enter fullscreen mode Exit fullscreen mode

Choosing Between Them: One Rule

Did this view create the object, or receive it? Created it -> @StateObject. Received it as a parameter -> @ObservedObject. And if it's not a class at all, just a value type owned by this view -> @State.

If you're targeting iOS 17 or later, Apple's newer @Observable macro replaces ObservableObject/@Published entirely and removes the @StateObject-vs-@ObservedObject distinction.


Originally published on Tech Android Network, where I write Android, iOS, Flutter and game-dev tutorials.

Top comments (1)

Collapse
 
mosmox_studio profile image
Mosmox Studio •

One pitfall worth adding to the @observable note: with @State private var model = Model(), the initializer isn't lazy like @StateObject's autoclosure. Model() runs every time the parent recreates the view struct; SwiftUI keeps the first instance and throws the new ones away. Harmless for a cheap init, but if init starts a network call or a timer you'll see it fire repeatedly. Moving that work into .task fixes it.