0

我正在尝试学习分布式通知。这是我是否无法在 MacOS 中捕获通知的代码。

import SwiftUI

@main
class NotificationsDemoApp: App {
    
    required init(){
        let nc = NotificationCenter.init()
        nc.post(name: NSNotification.Name.init(rawValue: "TEST"), object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(notified(notification:)), name: NSNotification.Name.init(rawValue: "TEST"), object: nil)
    }
    
    @objc func notified(notification : NSNotification){
        debugPrint("Inside Notification")
    }

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

我希望函数notified应该被调用。怎么做?谢谢。

4

0 回答 0