【问题标题】:Swift 4 - Notification Center addObserver issueSwift 4 - 通知中心 addObserver 问题
【发布时间】:2023-03-28 10:05:01
【问题描述】:

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

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

sayHello() 方法非常简单 - 如下所示:

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

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

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

【问题讨论】:

  • 如果你使用了#selector,编译器就会指出问题所在——sayHello 必须是@objc。比较stackoverflow.com/q/44390378/2976878
  • 像这样使用#selector(yourVC.yourfunctionName)
  • 正如@Hamish 已经提到的,您应该使用#selector(sayHello) 和您的方法签名,您还应该传递通知对象(删除NS 前缀)@objc func sayHello(_ notification: Notification)
  • @Mannopson 不需要传递视图控制器前缀。顺便说一句,它应该总是以大写字母开头
  • @LeoDabus 我总是使用self 而不是viewControllerName。示例:#selector(self.functionName)

标签: swift nsnotificationcenter xcode9 addobserver


【解决方案1】:

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

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

解决了问题

【讨论】:

  • 姓名不是通知的成员 - 问题即将在 swift 4 中出现
【解决方案2】:
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")
}

【讨论】:

  • 如果你以这种方式编写代码,当视图消失时(例如,当另一个视图被推送到导航堆栈时),观察者将被移除,但如果新视图被弹回原始视图(其中观察者曾经生活),没有通知观察者。最好将addObserver 放入viewWillAppear。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 2018-08-09
    • 1970-01-01
    • 2014-07-25
    相关资源
    最近更新 更多