Swift 4 - 通知中心addObserver问题

时间:2017-10-11 13:38:16

标签: swift nsnotificationcenter xcode9 addobserver

每次unrecognized selector到达且应用程序尝试执行其关联方法时,我都会崩溃并收到Notification错误。 这是我的代码 - 位于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引导我完成必要的代码修复/更新] - 但崩溃仍在继续发生。)

3 个答案:

答案 0 :(得分:28)

您可以使用以下步骤改进代码:

"message": [
  {
    "_id": "59415f148911240fc812d393",
    "email": "jane.doe@foo.de",
    "fullName": "Jane Doe",
    "__v": 0,
    "created": "2017-06-14T16:06:44.457Z"
  },
  {
    "_id": "5943b80be8b8b605686a67fb",
    "email": "john.doe@foo.de",
    "fullName": "John Doe",
    "__v": 0,
    "created": "2017-06-16T10:50:51.180Z"
  }
]

并像这样使用它:

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

但正如已经指出的那样,通过改为 #selector

来解决问题

答案 1 :(得分:6)

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")
}

答案 2 :(得分:2)

Swift 4.0和Xcode 9.0 +:

发送(发布)通知:

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

OR

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) {}

Swift 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)
}

Swift 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