Using selector in Swift 3 NotificationCenter observer

帅比萌擦擦* 提交于 2019-12-02 10:11:34

问题


NotificationCenter.default.addObserver(self, selector: Selector(("uploaded")), name: NSNotification.Name(rawValue: "uploaded"), object: nil)

I was writing name: "uploaded:" and xcode corrected it to the above code. The problem is when running the app i get unrecognized selector.

Any one know how to fix this to work with swift 3


回答1:


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




回答2:


Use the (identifier checking) #selector syntax:

Without parameter:

#selector(uploaded)

With parameter:

#selector(uploaded(_:))


来源:https://stackoverflow.com/questions/39984164/using-selector-in-swift-3-notificationcenter-observer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!