我是Swift的新手。下面有两个代码片段:
NotificationCenter.default.addObserver(self,
selector:#selector(ViewController.notificationReceived),
name: Notification.Name(rawValue: name), object: nil)
@objc func notificationReceived(notification:Notification){
let x = notification.userInfo!
print("\(x["name"]!)")
}
最后
let x:UITapGestureRecognizer = UITapGestureRecognizer(target: self,
action: #selector(tapped))
self.addGestureRecognizer(x)
func tapped(){
print("tapped")
self.delegate!.theViewTapped()
}
为什么在 notificationCenter
中,我需要为 selector
参数提供 @objc
标签,但不需要为 UITapGestureRecognizer
的 action 参数提供?在 Swift 中,
Selector
和 Action
之间的确切区别是什么?
@objc
。 - Hamish