🌱 Observable objects, environment objects, and @Published
-
https://www.hackingwithswift.com/quick-start/swiftui/adding-items-to-an-order-with-environmentobject
-
https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-views-in-a-loop-using-foreach
-
Use @State for simple properties that belong to a single view. They should usually be marked private.
-
Use @ObservedObject for complex properties that might belong to several views. Most times you’re using a reference type you should be using @ObservedObject for it.
-
Use @StateObject once for each observable object you use, in whichever part of your code is responsible for creating it.
-
Use @EnvironmentObject for properties that were created elsewhere in the app, such as shared data.
Of the four you will find that @ObservedObject is both the most useful and the most commonly used, so if you’re not sure which to use start there.