Using selector in Swift 3 NotificationCenter observer

后端 未结 2 1364
慢半拍i
慢半拍i 2021-01-23 10:43
NotificationCenter.default.addObserver(self, selector: Selector((\"uploaded\")), name: NSNotification.Name(rawValue: \"uploaded\"), object: nil)

I was

相关标签:
2条回答
  • 2021-01-23 10:59

    Use the (identifier checking) #selector syntax:

    Without parameter:

    #selector(uploaded)
    

    With parameter:

    #selector(uploaded(_:))
    
    0 讨论(0)
  • 2021-01-23 11:12
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.update), name: NSNotification.Name(rawValue: "uploaded"), object: nil)
    
    func update() {
          // do what you want
       }
    

    please note that "ViewController" is the class name where your function is

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