14

unrecognized selector每次 aNotification到达并且应用程序尝试执行其关联方法时,我都会崩溃并收到错误。这是我的代码-位于viewDidLoad

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: Selector(("sayHello")), name:NSNotification.Name(rawValue: "dataDownloadCompleted"), object: nil)

sayHello()方法非常简单——看起来像这样:

func sayHello() {
    print("Hello")
}

我已验证已Notification成功发布并且已成功到达- 所以这不是问题。当应用程序Notification通过执行该方法来执行该sayHello()方法时,就会发生崩溃。它一直给我这个unrecognized selector错误。

任何想法我做错了什么?(顺便说一句,这与 Swift 3 和 Xcode 8 完美配合,但现在使用 Swift 4 和 Xcode 9,语法发生了变化 [Xcode 引导我完成了必要的代码修复/更新] - 但崩溃仍在发生。)

4

3 回答 3

32

您可以通过以下步骤改进您的代码:

extension Notification.Name {
    static let dataDownloadCompleted = Notification.Name(
       rawValue: "dataDownloadCompleted")
}

并像这样使用它:

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self,
                               selector: #selector(YourClass.sayHello),
                               name: .dataDownloadCompleted,
                               object: nil)

但正如已经指出的那样,通过更改为#selector来解决问题

于 2017-10-11T13:52:02.933 回答
10
Data Receiving - Add observer:

override func viewDidLoad() {
     super.viewDidLoad()
     NotificationCenter.default.addObserver(self, selector: #selector(yourfunction(notfication:)), name: .postNotifi, object: nil)
}

@objc func yourfunction(notfication: NSNotification) {
     print("xxx")
}

Sending Data - Post Notification:

override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)
      NotificationCenter.default.removeObserver(self, name: .postNotifi, object: nil)
}

extension Notification.Name {
      static let postNotifi = Notification.Name("postNotifi")
}
于 2018-03-12T06:30:41.430 回答
7

斯威夫特 4.0 和 Xcode 9.0+:

发送(发布)通知:

NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)

或者

NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil, userInfo: ["Renish":"Dadhaniya"])

接收(获取)通知:

NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)

收到通知的函数方法处理程序:


@objc func methodOfReceivedNotification(notification: Notification) {}

斯威夫特 3.0 和 Xcode 8.0+:

发送(发布)通知:


NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)

接收(获取)通知:


NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)

收到通知的方法处理程序:

func methodOfReceivedNotification(notification: Notification) {
  // Take Action on Notification
}

删除通知:

deinit {
  NotificationCenter.default.removeObserver(self, name: Notification.Name("NotificationIdentifier"), object: nil)
}

斯威夫特 2.3 和 Xcode 7:

发送(发布)通知

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)

接收(获取)通知


NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)

收到通知的方法处理程序

func methodOfReceivedNotification(notification: NSNotification){
  // Take Action on Notification
}

参考:https ://medium.com/@javedmultani16/notification-in-swift-4-8b0db631f49d

于 2019-08-24T22:14:41.413 回答