|
| 1 | +# 250702 main, UIApplication, UIKit, SceneDelegate |
| 2 | + |
| 3 | +`@main` vs `@UIApplicationMain` |
| 4 | + |
| 5 | +7์ 2์ผ (์) |
| 6 | + |
| 7 | +## ๋ฐฐ๊ฒฝ |
| 8 | + |
| 9 | +SwiftUI์์๋ `@main`์ ์ฌ์ฉํด ์ฑ์ ์ง์
์ ์ ์ ์ํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด๋ฉฐ, UIKit์์๋ ๊ธฐ์กด์ `@UIApplicationMain` ๋์ `@main` ์ฌ์ฉ์ด ์ ์ ๊ถ์ฅ๋๋ ์ถ์ธ๋ค. |
| 10 | + |
| 11 | +Swift Evolution ์ ์์(SE-0383)์ ๋ฐ๋ผ `@UIApplicationMain`์ deprecated ๋์์ผ๋ก ๊ฐ์ฃผ๋๋ฉฐ, ํฅํ ์ ๊ฑฐ๋ ๊ฐ๋ฅ์ฑ์ด ์๋ค. |
| 12 | + |
| 13 | +## ํต์ฌ ํฌ์ธํธ |
| 14 | + |
| 15 | +### 1. @main vs @UIApplicationMain |
| 16 | +- `@UIApplicationMain`: UIKit ์ ์ฉ, main.swift ์์ด ์ฑ์ ์์ํ ์ ์๋๋ก UIApplicationMain(...) ํธ์ถ ์ฝ๋๋ฅผ ์๋ ์์ฑ. ๋จ, SwiftUI๋ ์ปค์คํ
์ง์
์ ์๋ ์ฌ์ฉํ ์ ์์. |
| 17 | +- `@main`: Swift ํ์ค ์์ฑ, ์ด๋ค ํ์
(struct/class/enum)์ด๋ ์ฑ์ ์ง์
์ ์ผ๋ก ์ง์ ๊ฐ๋ฅ |
| 18 | + |
| 19 | +> @main์ Swift 5.3 ์ด์์์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ, Xcode 12 ์ด์์ด ํ์ํ๋ค. |
| 20 | +
|
| 21 | +### 2. UIKit + @main ์ฌ์ฉ ์ ์ฃผ์์ฌํญ |
| 22 | +UIKit์์ `@main`์ ์ฌ์ฉํ ๋๋ **๋ณ๋์ main ํจ์ ๊ตฌํ์ด ๋ถํ์**ํ๋ค. |
| 23 | + |
| 24 | +```swift |
| 25 | +@main |
| 26 | +class AppDelegate: UIResponder, UIApplicationDelegate { |
| 27 | + // Swift ์ปดํ์ผ๋ฌ๊ฐ UIApplicationMain(...)์ ์๋์ผ๋ก ์์ฑ ๋ฐ ํธ์ถํจ |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### 3. main ํจ์๊ฐ ํ์ํ ๊ฒฝ์ฐ |
| 32 | + |
| 33 | +main ํจ์๋ฅผ ์ง์ ๊ตฌํํด์ผ ํ๋ ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ๋ค: |
| 34 | + |
| 35 | +- ์ปค์คํ
UIApplication ์๋ธํด๋์ค ์ฌ์ฉ |
| 36 | +- ์ฑ ์์ ์ ํน๋ณํ ์ด๊ธฐํ ๋ก์ง ํ์ |
| 37 | +- ๋ฐํ์์ AppDelegate ํด๋์ค๋ฅผ ๋์ ์ผ๋ก ๊ฒฐ์ |
| 38 | + |
| 39 | +```swift |
| 40 | +import UIKit |
| 41 | + |
| 42 | +class CustomApplication: UIApplication { |
| 43 | + // ์ปค์คํ
๋ก์ง |
| 44 | +} |
| 45 | + |
| 46 | +// @main์ ์ฌ์ฉํ์ง ์๊ณ , ๋ณ๋์ main.swift ํ์ผ์์ ์ง์
์ ์ง์ ์ ์ |
| 47 | +UIApplicationMain( |
| 48 | + CommandLine.argc, |
| 49 | + CommandLine.unsafeArgv, |
| 50 | + NSStringFromClass(CustomApplication.self), |
| 51 | + NSStringFromClass(AppDelegate.self) |
| 52 | +) |
| 53 | +``` |
| 54 | + |
| 55 | +### 4. Scene-based Lifecycle๊ณผ์ ํธํ์ฑ |
| 56 | +`@main`์ SceneDelegate ๊ธฐ๋ฐ์ ์ฑ ๊ตฌ์กฐ์์๋ ์ ์ ์๋ํ๋ฉฐ, ๋ค์ ๋ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋๋ฉด SceneDelegate๊ฐ ์๋ ํธ์ถ๋๋ค: |
| 57 | + |
| 58 | +1. `AppDelegate`๊ฐ Scene ์์ฑ์ ์ํ `configurationForConnecting` ๋ฉ์๋๋ฅผ ๊ตฌํํ๊ณ ์๊ณ |
| 59 | +2. Info.plist์ `UIApplicationSceneManifest` ์ค์ ์ด ํฌํจ๋์ด ์์ ๊ฒ |
| 60 | + |
| 61 | +```swift |
| 62 | +@main |
| 63 | +class AppDelegate: UIResponder, UIApplicationDelegate { |
| 64 | + func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { |
| 65 | + return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +```xml |
| 71 | +<key>UIApplicationSceneManifest</key> |
| 72 | +<dict> |
| 73 | + <key>UIApplicationSupportsMultipleScenes</key> |
| 74 | + <true/> |
| 75 | + <key>UISceneConfigurations</key> |
| 76 | + <dict> |
| 77 | + <key>UIWindowSceneSessionRoleApplication</key> |
| 78 | + <array> |
| 79 | + <dict> |
| 80 | + <key>UISceneConfigurationName</key> |
| 81 | + <string>Default Configuration</string> |
| 82 | + <key>UISceneDelegateClassName</key> |
| 83 | + <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string> |
| 84 | + </dict> |
| 85 | + </array> |
| 86 | + </dict> |
| 87 | +</dict> |
| 88 | +``` |
| 89 | + |
| 90 | +## ๊ฒฐ๋ก |
| 91 | +UIKit์์ `@main` ์ฌ์ฉ ์ ๋๋ถ๋ถ์ ๊ฒฝ์ฐ main ํจ์๋ฅผ ๋ณ๋๋ก ๊ตฌํํ ํ์๊ฐ ์์ผ๋ฉฐ, ์ด๋ ์ฝ๋๋ฅผ ๋ ๊ฐ๊ฒฐํ๊ณ ํ๋์ ์ผ๋ก ๋ง๋ค์ด์ค๋ค. ๋จ, ํน๋ณํ ์ปค์คํฐ๋ง์ด์ง์ด ํ์ํ ๊ฒฝ์ฐ์๋ง main ํจ์๋ฅผ ์ง์ ๊ตฌํํ๋ฉด ๋๋ค. |
| 92 | + |
| 93 | +## ์ฐธ๊ณ ์๋ฃ |
| 94 | +- [Apple Documentation - App Structure](https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle) |
| 95 | +- [Swift Evolution - SE-0281](https://github.com/apple/swift-evolution/blob/main/proposals/0281-main-attribute.md) |
| 96 | +- [Swift Evolution - SE-0383](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0383-deprecate-uiapplicationmain-and-nsapplicationmain.md) |
0 commit comments