Jaebi의 Binary는 호남선

SwiftUI - App Protocol 본문

Swift

SwiftUI - App Protocol

jaebijae 2024. 7. 21. 18:35

목차 Open

App

  • 앱의 구조와 동작을 나타내는 Protocol
  • app의 content를 정의하는 body computed property 필요
  • @main annotation으로 entry point 지정 → 모든 app file에는 하나의 entry point
  • bodyScene 프로토콜을 준수하며 각 scene은 view hierarchy의 rootview와 lifecycle을 가지고 있음
  • 여기서 모든 scene에서 공유할 수 있는 state를 선언 할수 있음
    • @StateObject / @ObservedObject attribute로 model을 만들고 view input으로 ObservedObject, 또는 EnvironmentObject로 scene에 넣을수 있음
  • 예시:
@main
struct MyApp: App {
    @StateObject private var sharedModel = SharedModel()
    var body: some Scene {
        WindowGroup {
            RootView()
                .environmentObject(sharedModel)
        }
    }
}
  • @main, App, Scene, WindowGroup다 SwiftUI 앱의 Lifecycle과 관련이 있음

Reference

 

App | Apple Developer Documentation

A type that represents the structure and behavior of an app.

developer.apple.com

 

'Swift' 카테고리의 다른 글

AppDelegate & SceneDelegate  (0) 2024.07.21
App Lifecycle  (0) 2024.07.21
ARC (Automatic Reference Counting)  (0) 2024.07.20
Swift - Memory 기초  (0) 2024.07.20
Swift - 문법 (접근 제어)  (0) 2024.07.20