Menu

1 / 2

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

DEV Community: swift·Tech Stack·6 days ago
#kjZ7KNnB
#devtoswift#devto#dev#view#state
Long article (476 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.