【Xcode 12】macOS SwiftUI App AppDelegateを作る

Pocket

Xcode 12 は 「Life Cycle: SwiftUI App」と言う設定ができるようになりました。

Life Cycle 「SwiftUI App」

protocol App継承で下記のようなシンプルな構成になりました。そして、AppDelegate.swiftがなくなりました。

@main
struct プロジェクト名: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

AppDelegateが必要な場合は、下記のようなAppDelegateクラスを用意します。

import Foundation
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {

    }
    
    func applicationWillTerminate(_ aNotification: Notification) {
        
    }
}

struct プロジェクト名:App{}側に@NSApplicationDelegateAdaptor()を追記してやります。

@main
struct プロジェクト名: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED