Swift 4 - 通知中心 addObserver 问题
Swift 4 - Notification Center addObserver issue
每次 Notification
到达并且应用程序尝试执行其关联方法时,我都会崩溃并收到 unrecognized selector
错误。
这是我的代码 - 在 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 walked我通过必要的代码 fixes/updates] - 但崩溃不断发生。)
您可以通过以下步骤改进您的代码:
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)
但是正如已经指出的那样,通过更改为 #selector
可以解决问题
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")
}
Swift 4.0 & Xcode 9.0+:
发送(Post)通知:
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) {}
Swift 3.0 & Xcode 8.0+:
发送(Post)通知:
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:
发送(Post)通知
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
每次 Notification
到达并且应用程序尝试执行其关联方法时,我都会崩溃并收到 unrecognized selector
错误。
这是我的代码 - 在 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 walked我通过必要的代码 fixes/updates] - 但崩溃不断发生。)
您可以通过以下步骤改进您的代码:
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)
但是正如已经指出的那样,通过更改为 #selector
可以解决问题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")
}
Swift 4.0 & Xcode 9.0+:
发送(Post)通知:
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) {}
Swift 3.0 & Xcode 8.0+:
发送(Post)通知:
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:
发送(Post)通知
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