Swift 4 - 通知中心addObserver问题

Sir*_*b33 12 addobserver nsnotificationcenter swift xcode9

unrecognized selector每次Notification到达时我都会崩溃并收到错误,而App会尝试执行其关联的方法.这是我的代码 - 在viewDidLoad:

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

sayHello()方法是相当简单的-看起来是这样的:

func sayHello() {
    print("Hello")
}
Run Code Online (Sandbox Code Playgroud)

我已经确认已Notification成功发布并且成功到达 - 所以这不是问题.当App试图Notification通过执行该sayHello()方法时到达时,就会发生崩溃.它一直给我这个unrecognized selector错误.

我有什么想法我做错了吗?(顺便说一句,这与Swift 3和Xcode 8完美配合,但现在使用Swift 4和Xcode 9语法已经改变了[Xcode让我完成了必要的代码修复/更新] - 但崩溃仍在继续.)

Vla*_*tko 30

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

extension Notification.Name {
    static let dataDownloadCompleted = Notification.Name(
       rawValue: "dataDownloadCompleted")
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

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

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


Gia*_*ang 7

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")
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您以这种方式执行代码,则当视图消失时(例如,将另一个视图推入导航堆栈时)将删除观察者,但是如果将新视图弹出回到原始视图(观察者所在的位置),没有通知观察者。最好将`addObserver`放入`viewWillAppear`中。 (2认同)

Mr.*_*ani 7

Swift 4.0 和 Xcode 9.0+:

发送(发布)通知:

NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
Run Code Online (Sandbox Code Playgroud)

或者

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

Run Code Online (Sandbox Code Playgroud)

接收(获取)通知:

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

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


@objc func methodOfReceivedNotification(notification: Notification) {}
Run Code Online (Sandbox Code Playgroud)

Swift 3.0 和 Xcode 8.0+:

发送(发布)通知:


NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
Run Code Online (Sandbox Code Playgroud)

接收(获取)通知:


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

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

func methodOfReceivedNotification(notification: Notification) {
  // Take Action on Notification
}
Run Code Online (Sandbox Code Playgroud)

删除通知:

deinit {
  NotificationCenter.default.removeObserver(self, name: Notification.Name("NotificationIdentifier"), object: nil)
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特 2.3 和 Xcode 7:

发送(发布)通知

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
Run Code Online (Sandbox Code Playgroud)

接收(获取)通知


NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)
Run Code Online (Sandbox Code Playgroud)

接收通知的方法处理程序

func methodOfReceivedNotification(notification: NSNotification){
  // Take Action on Notification
}
Run Code Online (Sandbox Code Playgroud)

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