Views, Scenes, and Apps
Beside
Views, from this year SwiftUI can declareScenes andAppsViews are the basic building blocks, rendering everything on screen.Views can be composed with otherViews.Views form the content of aScene, making them platform-independentScenes can also be composed with otherScenes (think of tabs on macOS)Scenes form the content of anApp
Example
@main
struct MyApp: App {
@StateObject private var store = Store()
var body: some Scene {
WindowGroup {
MyView(store: store)
}
}
}In thie example we have an
App,MyApp, which contains aScene,WindowGroup, which contains a viewMyView.Above the struct declaration we have a new Swift 5.3 attribute
@main, which declares thisstructas the entry point of our programs execution.Declaring
Apps is very similar to declaringViews: both comform to a protocol (AppvsView), and both need to define abody(of typesome Scenevssome View).Like
Views, also Apps can declare data depenencies, and in this exampleMyAppdeclares to be owner of thestoreobject by using theStateObjectproperty wrapper,
WindowGroup
Scene that allows us to define the primary interface of our app.
WindowGroupis a scene that allows our app to automatically create multiple scenes containingMyViewwhen needed (this works for having multple windows on both iPad and mac).Like in UIKit, an app can provide a shared model for each scene to use, but the state of the views in those scenes will be independent.
WindowGroupalso manages tabs for us on macOS.@SceneStorageis a new property wrapper that manages scenes restoration.
More New features
DocumentGroup: New scene that help us with document based apps, more on this inBuild Document-Based Apps in SwiftUIsession.Settingsis a macOS-only scene used for declaring apps preferencesThe
CommandsAPI (along with the.commandsmodifier) let us describe commands to be shown in the main menu on macOS and key commands on iOS.
