Skip to content

Difference between @State, @StateObject, @EnvironmentObject, @ObservedObject, @Binding, @AppStorage, @Published and ObservableObject #35

@8bitzz

Description

@8bitzz

Example code with explanation

@StateObject

struct ContentView: View {
    @StateObject var person = Person()
    
    var body: some View {
        VStack {
            PersonView(person: person)
            PersonEditorView(person: $person)
        }
    }
}
  • Overall, this code demonstrates how to share data between different SwiftUI views: ContentView, PersonView, and PersonEditorView
  • The ContentView owns a Person object and passes it to two different subviews.
  • The @StateObject property wrapper is used to indicate that the person property is owned by the ContentView. This means that any changes to the person object will cause the ContentView view to be re-rendered.

How to propagate changes from a child view to its parent view

@ Binding

struct PersonEditorView: View {
    @Binding var person: Person
    
    var body: some View {
        TextField("Enter name", text: $person.name)
    }
}
  • The PersonEditorView subview uses a @Binding to modify the name property of the Person object, and the changes are automatically reflected in the PersonView subview.
  • The @Binding property wrapper means that the PersonEditorView does not have its own state, but rather make change to the person object that owned by the ContentView

ObservableObject, @published, and @ObservedObject

import SwiftUI

class Person: ObservableObject {
    @Published var name: String = "John"
}

struct PersonView: View {
    @ObservedObject var person: Person
    
    var body: some View {
        Text(person.name)
    }
}
  • The class Person conform the ObservableObject protocol - which is a Protocol that you can adopt to create objects that can be observed for changes. In this example, it means an instance of the Person object can be observed for changes.
  • The @Published is a property wrapper that you can use to mark a property as being observable for changes. Since the property name is marked as @Published, you are telling Swift that any changes to the name value should be automatically published to any subscribers.
  • The @ObservedObject means you subscribe to changes in the instance of an ObservableObject. When you mark the property person with @ObservedObject, SwiftUI will automatically update PersonView that depend on the object's properties whenever the object person changes.

ObservableObject

  • a Protocol that you can adopt to create objects that can be observed for changes.

@ Published

  • a Property Wrapper that you can use to mark a property as being observable for changes.
  • When you mark a property with @Published, you tell Swift that any changes to the property's value should be automatically published to any subscribers.

@ObservedObject

  • a Property Wrapper that you can use to subscribe to changes in an instance of an ObservableObject.

  • When you mark a property with @ObservedObject, SwiftUI will automatically update any views that depend on the object's properties whenever the object changes.

  • The @ObservedObject property wrapper tells Swift to automatically update the view whenever the value of the name property changes.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions