【发布时间】:2017-06-30 00:07:35
【问题描述】:
Swift 新手。我在下面有两个 sn-ps:
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 操作参数提供标签?
Selector 和Action in Swift 到底有什么区别?
【问题讨论】:
-
比较How can I deal with @objc inference deprecation with #selector() in Swift 4?——正如OOPer所说,两个例子都要求目标方法是
@objc。