每当一个unrecognized selector到达并且应用程序尝试执行它相关的方法时,我都会崩溃并得到一个Notification错误。这是我的代码--在viewDidLoad中
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: Selector(("sayHello")), name:NSNotification.Name(rawValue: "dataDownloadCompleted"), object: nil)sayHello()方法非常简单-如下所示:
func sayHello() {
print("Hello")
}我已经证实了Notification已经成功发布,并且它已经成功到达--所以这不是问题所在。当应用程序在Notification到达时(通过执行sayHello()方法)采取行动时,就会发生崩溃。它一直给我unrecognized selector错误。
知道我做错什么了吗?(顺便说一句,这在Swift 3& Xcode 8中非常有效,但是现在对于Swift 4和Xcode 9,语法已经改变了,Xcode帮助我完成了必要的代码修复/更新--但是崩溃仍在发生。)
发布于 2017-10-11 13:52:02
您可以通过以下步骤改进代码:
extension Notification.Name {
static let dataDownloadCompleted = Notification.Name(
rawValue: "dataDownloadCompleted")
}像这样使用它:
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self,
selector: #selector(YourClass.sayHello),
name: .dataDownloadCompleted,
object: nil)但是,正如已经指出的,通过更改为#选择器可以解决问题
发布于 2018-03-12 06:30:41
Data Receiving - Add observer:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(yourfunction(notfication:)), name: .postNotifi, object: nil)
}
@objc func yourfunction(notfication: NSNotification) {
print("xxx")
}
Sending Data - Post Notification:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: .postNotifi, object: nil)
}
extension Notification.Name {
static let postNotifi = Notification.Name("postNotifi")
}发布于 2019-08-24 22:14:41
SWIFT4.0& Xcode 9.0+:
发送(邮寄)通知:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)或
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil, userInfo: ["Renish":"Dadhaniya"])接收(获取)通知:
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)接收通知的函数-方法处理程序:
@objc func methodOfReceivedNotification(notification: Notification) {}SWIFT3.0& Xcode 8.0+:
发送(邮寄)通知:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)接收(获取)通知:
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)接收通知的方法处理程序:
func methodOfReceivedNotification(notification: Notification) {
// Take Action on Notification
}删除通知:
deinit {
NotificationCenter.default.removeObserver(self, name: Notification.Name("NotificationIdentifier"), object: nil)
}Swift 2.3 & Xcode 7:
发送(邮寄)通知
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)收到(收到)通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)接收通知的方法处理程序
func methodOfReceivedNotification(notification: NSNotification){
// Take Action on Notification
}参考文献:https://medium.com/@javedmultani16/notification-in-swift-4-8b0db631f49d
https://stackoverflow.com/questions/46689693
复制相似问题