Swift 4 - Notification Center addObserver issue

后端 未结 3 1319
梦谈多话
梦谈多话 2020-12-30 05:10

I\'m crashing and getting an unrecognized selector error every time a Notification arrives and the App tries to execute its associated method. Here

相关标签:
3条回答
  • 2020-12-30 05:22
    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")
    }
    
    0 讨论(0)
  • 2020-12-30 05:29

    You can improve your code with these steps:

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

    And use it like this:

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

    But as was already pointed out, issue is solved by changing to #selector

    0 讨论(0)
  • 2020-12-30 05:39

    Swift 4.0 & Xcode 9.0+:

    Send(Post) Notification:

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

    OR

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

    Receive(Get) Notification:

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

    Function-Method handler for received Notification:

    
    @objc func methodOfReceivedNotification(notification: Notification) {}
    

    Swift 3.0 & Xcode 8.0+:

    Send(Post) Notification:

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

    Receive(Get) Notification:

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

    Method handler for received Notification:

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

    Remove Notification:

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

    Swift 2.3 & Xcode 7:

    Send(Post) Notification

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

    Receive(Get) Notification

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

    Method handler for received Notification

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

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

    0 讨论(0)
提交回复
热议问题