Blog by Frank

iOS Lifecycle

Delegate

What is a delegate in iOS?

a delegate is any object that should be notified when something interesting has happened. What that “something interesting” means depends on the context: for example, a table view’s delegate gets notified when the user taps on a row, whereas a navigation controller’s delegate gets notified when the user moves between view controllers.

Lifecycle

UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]))

但是Swift项目中没有一个名为main.swift的文件,为什么app的入口没有了?官方文档的说法是这样的:

In Xcode, Mac templates default to including a “main.swift” file, but for iOS apps the default for new iOS project templates is to add @UIApplicationMain to a regular Swift file. This causes the compiler to synthesize a mainentry point for your iOS app, and eliminates the need for a “main.swift” file.

在Xcode 12中"@main“是什么意思?

What does “@main” mean in Xcode 12?

Xcode 12支持在UIKit或基于AppKit的应用程序中使用@main代替@UIApplicationMain或@NSApplicationMain。

AppDelegate SwiftUI

How to add an AppDelegate to a SwiftUI app

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("Your code here")
        return true
    }
}

@main
struct NewIn14App: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}