Mới đối với Swift. Tôi có hai đoạn dưới đây:Selector vs Action Swift 4
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"]!)")
}
và cuối cùng
let x:UITapGestureRecognizer = UITapGestureRecognizer(target: self,
action: #selector(tapped))
self.addGestureRecognizer(x)
func tapped(){
print("tapped")
self.delegate!.theViewTapped()
}
Tại sao nó rằng đối với notificationCenter
? Tôi có nghĩa vụ cung cấp thẻ @objc
cho thông số selector
nhưng không cho tham số hoạt động UITapGestureRecognizer
?
Sự khác biệt chính xác giữa Selector
và Action in
Swift là gì?
So sánh [Làm thế nào tôi có thể đối phó với phản đối suy luận @objc với #selector() trong Swift 4?] (Https://stackoverflow.com/q/44390378/2976878) - như OOPer nói, cả hai ví dụ đều yêu cầu phương thức đích là '@ objc'. – Hamish