Im learning notification centers right now and am getting this error while trying to register an observer:
Cannot convert value of type 'Selector' to expected argument type 'String'
My observer code:
NotificationCenter.addObserver(self, forKeyPath: #selector(receivedMsg), options: Notification.Name("NC1"), context: nil)
Function receivedMsg:
@objc func receivedMsg() {
print("MSG Received")
}
Working off this tutorial: https://www.hackingwithswift.com/example-code/system/how-to-post-messages-using-notificationcenter
Why am I getting this error and what can I do to fix it? (Swift 4.2)
You'll need to fix two things:
Access an instance of NotificationCenter
with NotificationCenter.default
Use the addObserver
method signature available on NotificationCenter
Complete code should be something like
NotificationCenter.default.addObserver(self, selector: #selector(receivedMsg), name: Notification.Name("NC1"), object: nil)