Swift ApplicationIDFinishLaunching未被触发

Swift ApplicationIDFinishLaunching未被触发,swift,xcode,swiftui,Swift,Xcode,Swiftui,试图理解我在使用以下代码时犯了什么错误。 这将是一个Mac应用程序,Xcode版本12.4。我的问题是,applicationdFinishLaunching从未被触发 import SwiftUI @main struct TestApp: App { @NSApplicationDelegateAdaptor(AppDelegate.self) var Delegate var body: some Scene { WindowGroup {

试图理解我在使用以下代码时犯了什么错误。 这将是一个Mac应用程序,Xcode版本12.4。我的问题是,
applicationdFinishLaunching
从未被触发

import SwiftUI

@main
struct TestApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var Delegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}


class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        let alert = NSAlert.init()
        alert.messageText = "Hello world"
        alert.informativeText = "Information text"
        alert.addButton(withTitle: "OK")
        alert.addButton(withTitle: "Cancel")
        alert.runModal()
    }
}

你打字有错误。这不是“a”而是“a”

class AppDelegate:NSObject,NSApplicationDelegate{

func applicationdFinishLaunching(uu通知:通知){//首先,您可能想使用
UIAlertViewController
而不是
NSAlert
。其次,我认为您无法显示AppDelegate发出的警报,因为它不是
UIViewController
。您需要从
UIViewController
或您的SwiftUI视图中显示此警报。@Todd,谢谢。警报代码在其他地方工作,但我想你建议实际上正在调用ApplicationIDFinishLaunching,但这不是一个触发警报的好地方?我会仔细检查一下。谢谢你。@Todd这段代码是为MacOS编写的,不是为iOS编写的。@RajaKishan谢谢你指出这一点。我错过了。在这种情况下,同样的事情也适用于你想从
NSVi启动它的地方ewController
或SwiftUI视图,而不是应用程序委托。@Todd不。在mac中,可以使用alert运行alert。runModal()不需要viewcontroller。谢谢。差不多。我需要将a替换为u,然后将NSNotification替换为Notification。现在工作正常。非常感谢。
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) { //<here
        let alert = NSAlert.init()
        alert.messageText = "Hello world"
        alert.informativeText = "Information text"
        alert.addButton(withTitle: "OK")
        alert.addButton(withTitle: "Cancel")
        alert.runModal()
    }
}