Protocol¶
App¶
A type that represents the structure and behavior of an app.¶
Declaration¶
protocol App
Overview¶
Create an app by declaring a structure that conforms to the App protocol. Implement the required body computed property to define the app's content:
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
Text("Hello, world! 🌍🙋")
.font(.title)
}
}
}

Precede the structure's declaration with the @main attribute to indicate that your custom App protocol conformer provides the entry point into your app. The protocol provides a default implementation of the main() method that the system calls to launch your app. You can have exactly one entry point among all of your app's files.
Compose the app's body from instances that conform to the Scene protocol. Each scene contains the root view of a view hierarchy and has a life cycle managed by the system. SwiftUI provides some concrete scene types to handle common scenarios, like for displaying documents or settings. You can also create custom scenes.
@main
struct Mail: App {
var body: some Scene {
WindowGroup {
MailViewer()
}
}
}
struct MailViewer: View {
var body: some View {
// Implement here
Text("Turn me into a mail app! 📨")
}
}
@main
struct Mail: App {
@StateObject private var model = MailModel()
var body: some Scene {
WindowGroup {
MailViewer()
.environmentObject(model) //Passed through the environment.
}
#if os(iOS)
Settings {
SettingsView(model: model) //Passed as an observed object.
}
#endif
}
}
struct MailViewer: View {
@EnvironmentObject var model: MailModel
var body: some View { Text("Implement me! 📬") }
}
struct SettingsView: View {
@ObservedObject var mdoel: MailModel
var body: some View { Text("Implement me! ⚙️") }
}
class MailModel: ObservableObject { /\* implement here \*/ }
Availability¶
iOS 14.0+
macOS 11.0+
tvOS 14.0+
watchOS 7.0+
Topics¶
Instance Property¶
body The content and behavior of the app.
Associated Type¶
Body The type of scene representing the content of the app.
Type Method¶
main() Initializes and runs the app.
Initializer¶
init() Creates an instance of the app using the body that you define for its content.