添加观察者而不使用选择器

问题描述 投票:0回答:1

我需要在项目中添加观察值,但是由于与我一起使用的管理器不允许我在函数中使用@objc,因此我是否可以不使用@objc而使用此函数?

   func createObservers() {
      NotificationCenter.default.addObserver(self, selector: #selector(self.updatedata(notification:)),
                                             name: Notification.Name(rawValue: updateNotificationKey), object: nil)

    }

   @objc dynamic func updatedata(notification: NSNotification) {
         updateDataIcon()
     }

您的帮助将不胜感激。

swift observer-pattern
1个回答
0
投票

您可以将其与内联块一起使用

NotificationCenter.default.addObserver(forName:  Notification.Name(rawValue: updateNotificationKey) , object: nil, queue: .main) { [weak self] notification in 

}
© www.soinside.com 2019 - 2024. All rights reserved.