I created a Model like this:
class TestModel: ObservableObject {
@Published var num: Int = 0
}
Model is be used in "Home" view and "Home"s child view "HomeSub"
struct Home: View {
@StateObject var model = TestModel()
var body: some View {
NavigationView(content: {
NavigationLink(destination: HomeSub(model: model)) { Text("\(model.num)") }
})
}
}
struct HomeSub: View {
//1
@StateObject var model = TestModel()
//2
@ObservedObject var model = TestModel()
var body: some View {
VStack {
Text("\(model.num)")
.padding()
.background(Color.red)
Button("Add") {
model.num += 1
}
}
.onChange(of: model.num, perform: { value in
print("homeSub: \(value)")
})
}
}
In HomeSub view, what is the difference between 1 and 2? When I run the project, they have exactly the same behavior.