NSNotificationCenter addObserver in Swift while call a private method

前端 未结 2 1337
离开以前
离开以前 2021-02-19 19:15

I use the addObserver API to receive notification:

NSNotificationCenter.defaultCenter().addObserver(self, selector: \"methodOFReceivedNotication:\",         


        
相关标签:
2条回答
  • 2021-02-19 20:09

    Just mark it with the dynamic modifier or use the @objc attribute in your method declaration

    dynamic private func methodOFReceivedNotication(notification: NSNotification){
        //Action take on Notification
    }
    

    or

    @objc private func methodOFReceivedNotication(notification: NSNotification){
        //Action take on Notification
    }
    
    0 讨论(0)
  • 2021-02-19 20:09

    Have you considered using -addObserverForName:object:queue:usingBlock:?

    NSNotificationCenter.defaultCenter().addObserverForName("NotificationIdentifier", object: nil, queue: nil, usingBlock: {
        [unowned self] note in
        self.methodOFReceivedNotication(note)
    })
    

    or instead of calling the private method, just performing the action.

    NSNotificationCenter.defaultCenter().addObserverForName("NotificationIdentifier", object: nil, queue: nil, usingBlock: {
        [unowned self] note in
        // Action take on Notification
    })
    
    0 讨论(0)
提交回复
热议问题