SwiftUI gives us a completely new way to lay out out user interfaces, in a declarative and responsive way. Your data dictates what is displayed. But this leads to a new problem - how should the data models be constructed and how can they be passed around between the various views that make up your app?
In this post, I intend to discuss the possibilities with examples.
Update - January 2021: I think the information in this post is still all valid except for one change. When you are initializing an ObservableObject, you should use @StateObject
instead of @ObservedObject
. Your views can receive objects that are owned by other views as @ObservedObject
or @EnvironmentObject
but the owner of the data should always create the data object with @StateObject
.