@StateObject is a property wrapper used to initialize a class object and store it in the view state in SwiftUI. This means that the object is stored as long as the view exists and is destroyed along with it. Typically, using @StateObject is more practical for class objects that are needed for multiple views, not just one. For example:
class UserData: ObservableObject {
@Published var name = "John"
@Published var age = 30
}
struct ContentView: View {
@StateObject var userData = UserData()
var body: some View {
NavigationView {
VStack {
Text("Name: \(userData.name)")
Text("Age: \(userData.age)")
NavigationLink(
destination: ProfileView(userData: userData),
label: {
Text("Edit Profile")
})
}
.navigationTitle("Home")
}
}
}
struct ProfileView: View {
@ObservedObject var userData: UserData
var body: some View {
Form {
TextField("Name", text: $userData.name)
Stepper("Age: \(userData.age)", value: $userData.age)
}
.navigationTitle("Profile")
}
}
In this example,UserData is an object of a class that contains several properties that can be used in multiple views. The class is marked as ObservableObject so it can be used with @StateObject and @ObservedObject.
In ContentView, we create a new UserData object using @StateObject to save the state between transitions between different views. In this case, ContentView displays user data, visualizes it, and contains a link to another view (ProfileView) that can be used to edit the user data.
In ProfileView, we get access to the same UserData object using @ObservedObject to modify user data. When the user changes data, it is automatically updated in ContentView because the same UserData object is used.
Note: Use @ObservedObject if you need to observe changes in a class object from one view and @StateObject if you need to save the state of a class object that affects the display of multiple views.
If you use @ObservedObject instead of @StateObject for an object needed in multiple views, each view will have its own instance of the object, which can lead to problems with data synchronization between views. Therefore, in this case, it is better to use @StateObject.