In SwiftUI, what does an @main App type declare, and how does a WindowGroup turn its content into windows?
answer
- one entry point per app
- body returns scenes, not views
- WindowGroup content is a template
- fresh @State storage per window
basics
~20 sA type conforming to App and marked @main is the app's single entry point, and its body returns scenes. A WindowGroup's content is a template: every window it creates gets its own view hierarchy and its own @State storage.
solid answer
~40 sThe `App` protocol describes the whole app: you mark one conforming struct with `@main`, and the protocol's default `main()` launches it. Its `body` is a scene builder that returns `Scene` values, not views. `WindowGroup` is the everyday scene: the view hierarchy you give it is a template, and each window SwiftUI creates from it is an independent instance, so `@State` and `@StateObject` storage is allocated per window. On iPhone that means one full-screen window; on iPadOS (with `UIApplicationSupportsMultipleScenes` set to `true`) and on macOS, people can open several. State that every window must share, such as a document store, belongs in the `App` itself and is handed down through the environment or an initializer.
code
swift · 28 linesimport Observation
import SwiftUI
@Observable
final class DocumentStore {
var titles: [String] = []
}
@main
struct EditorApp: App {
@State private var store = DocumentStore() // one per app
var body: some Scene {
WindowGroup {
EditorRoot()
.environment(store)
}
}
}
struct EditorRoot: View {
@Environment(DocumentStore.self) private var store
@State private var selectedIndex = 0 // one per window
var body: some View {
Text(store.titles.isEmpty ? "No documents" : store.titles[selectedIndex])
}
}go deeper
Recall the shape: one @main type conforming to App, a body that returns scenes, and a WindowGroup wrapping the first view.
Explain that a WindowGroup is a template and that each window gets new @State storage, then say where shared models must live.
Show you can place state deliberately: app-owned models injected through the environment, per-window selection in view state, launch work outside views.
Frame the App struct as the composition root: what a team allows there, how shared services are injected, and how that choice scales to multiple windows.
## What the App type is Every app built with the SwiftUI life cycle starts from one type that conforms to the **`App` protocol**. The protocol has two requirements: an `init()` and a `body` property whose type conforms to `Scene`. You put the **`@main` attribute** on that type to say "this is where the program starts", and the protocol supplies a default `main()` that the system calls to launch the app. Apple's documentation is explicit that you can have **exactly one entry point** among all of the app's files, so a second `@main` type is a compile error, not a second app. `App` is declared `@MainActor`, and a type that adopts it in its own declaration inherits that main-actor isolation. ## Scenes, not views The `body` of an `App` is marked with `@SceneBuilder` and returns **scenes**. A scene is a part of the user interface whose life cycle the system manages; it acts as the container for a view hierarchy. The system decides how a scene appears: it might fill the whole display, become a resizable window, or a tab in a window, depending on the platform and the scene type. That is why the body returns `some Scene` and why the first view you write sits *inside* a scene rather than at the top level. - `WindowGroup` is the scene almost every iOS app declares. - Other scene types exist for documents and, on the Mac, for single windows and settings; those are a separate topic. - Scenes take their own modifiers, such as `commands(content:)` or `onChange(of:initial:_:)`, the same way views take view modifiers. You can also write a **custom scene**: a type that conforms to `Scene` and whose own `body` returns other scenes, for example a `WindowGroup` together with its modifiers. That keeps a large `App` body readable, and it is the same composition idea as building a view from smaller views. The `App` body then lists the custom scene as if it were a built-in one. ## WindowGroup is a template A `WindowGroup` does not hold one window. The view hierarchy you pass it is a **template**, and every window the app creates from the group is a fresh instance of that template. Apple's documentation states the consequence directly: every window in the group maintains independent state, and the system allocates new storage for any `State` or `StateObject` variables in the scene's view hierarchy for each window it creates. | Platform | What the group produces | |---|---| | iPhone | One window that fills the display | | iPadOS | Several windows, but only when the Info.plist scene manifest sets `UIApplicationSupportsMultipleScenes` to `true` | | macOS | Several windows, which people can gather into tabs; standard window commands come for free | ## Where shared state lives Because every window owns its own copy of view state, anything that all windows must agree on has to live *above* the windows. The usual place is the `App` struct: 1. Create the shared model once in the app, for example `@State private var store = DocumentStore()` for an `@Observable` class, or `@StateObject` for an `ObservableObject`. 2. Pass it into the group's content through the environment, or through an initializer. 3. Keep per-window choices, such as which document a window is showing or which tab is selected, as `@State` in the window's views. Getting this split wrong produces two classic bugs: a "shared" model created in a root view turns into one model per window, and a "per-window" selection stored in the app makes every window jump together. ## Launch work A view's appearance hooks run once per window and again whenever the view reappears, so they are the wrong place for one-time setup. App-wide setup belongs with the app: in the `App` initializer, which runs when `main()` creates the app at launch, or in an app delegate adaptor when it needs a UIKit delegate callback. ## Mistakes interviewers listen for - Saying the `App` body returns the root view. It returns scenes; the root view is the content of a scene. - Assuming two windows from one group share `@State`. They do not. - Expecting multiple iPad windows without setting the scene-manifest key. - Creating a shared store inside the group's root view and wondering why each window has different data. - Putting `@main` on something other than the `App` conformer, or on two types at once.
- Where does one-time launch work, such as configuring a crash reporter, go in a SwiftUI app?Not in a view's onAppear or task, because those run per window and again when a view reappears. Put it in the App's initializer, which runs when main() creates the app at launch, or in an app delegate adaptor's launch callback when the work needs a UIKit delegate method. Keep it short, since it sits on the launch path.
- What happens to a view's @State when a person opens a second iPad window from the same WindowGroup?Nothing is shared. SwiftUI allocates new storage for every @State and @StateObject in the hierarchy of each new window, so the second window starts from initial values. Data both windows must see belongs in a model owned above the windows, typically in the App, and passed down through the environment.
- The @main App struct's body returns the root view directly.
- A WindowGroup always creates exactly one window on every platform.
- Two windows from the same WindowGroup share their @State values.
- Several types in one target can each be marked @main as alternative entry points.
- Creating the shared store in the root view's onAppear is fine because it runs once.