在 Swift 3 NotificationCenter 观察器中使用选择器

Using selector in Swift 3 NotificationCenter observer

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

我写的是 name: "uploaded:" 并且 xcode 将其更正为上面的代码。问题是当 运行 应用程序出现无法识别的选择器时。

任何人都知道如何解决这个问题 swift 3

使用(标识符检查)#selector 语法:

无参数:

#selector(uploaded)

带参数:

#selector(uploaded(_:))
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.update), name: NSNotification.Name(rawValue: "uploaded"), object: nil)

func update() {
      // do what you want
   }

请注意,"ViewController" 是 class 函数所在的名称