ObservedObject
struct · iOS 13.0+ · SDK 27.1
@MainActor @propertyWrapper @preconcurrency @frozen struct ObservedObject<ObjectType> where ObjectType : ObservableObjectA property wrapper type that subscribes to an observable object and invalidates a view whenever the observable object changes. Add the `@ObservedObject` attribute to a parameter of a SwiftUI ``View`` when the input is an <doc://com.apple.documentation/documentation/Combine/ObservableObject> and you want the view to update when the object's published properties change. You typically do this to pass a ``StateObject`` into a subview. The following example defines a data model as an observable object, instantiates the model in a view as a state object, and then passes the instance to a subview as an observed object: class DataModel: ObservableObject { @Published var name = "Some Name" @Published var isEnabled = false } struct MyView: View { @StateObject private var model = DataModel() var body: some View { Text(model.name) MySubView(model: model) } } struct MySubView: View { @ObservedObject var model: DataModel var body: some View { Toggle("Enabled", isOn: $model.isEnabled) } } When any published property of the observable object changes, SwiftUI updates any view that depends on the object. Subviews can also make updates to the model properties, like the ``Toggle`` in the above example, that propagate to other observers throughout the view hierarchy. Don't specify a default or initial value for the observed object. Use the attribute only for a property that acts as an input for a view, as in the above example. > Note: Don't wrap objects conforming to the <doc://com.apple.documentation/documentation/Observation/Observable> protocol with `@ObservedObject`. SwiftUI automatically tracks dependencies to `Observable` objects used within body and updates dependent views when their data changes. Attempting to wrap an `Observable` object with `@ObservedObject` may cause a compiler error, because it requires that its wrapped object to conform to the <doc://com.apple.documentation/documentation/Combine/ObservableObject> protocol. > > If the view needs a binding to a property of an `Observable` object in its body, wrap the object with the ``Bindable`` property wrapper instead; for example, `@Bindable var model: DataModel`. For more information, see <doc:Managing-model-data-in-your-app>.
Members (0)
This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.
Skipped declarations
4 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.
ObservedObject.init(initialValue:)
This declaration needs Swift generic type arguments. (no canonical witness for ObjectType)
ObservedObject.init(wrappedValue:)
This declaration needs Swift generic type arguments. (no canonical witness for ObjectType)
ObservedObject.wrappedValue
This declaration needs Swift generic type arguments. (no canonical witness for ObjectType)
ObservedObject.projectedValue
The containing type needs Swift generic type arguments. (no canonical witness for ObjectType)