Reputation: 325
I've read all of the possible solutions on stack overflow but not one works for me.
My code is
func foo() {
NotificationCenter.default.addObserver(self, selector: #selector(fetchedUser(notification:)) , name: NSNotification.Name.init("dbReady"), object: nil)
loggedUser.fetchUserByUID(id: current.uid)
return true
}
func fetchedUser(notification:NSNotification){
let info = notification.object as! [String : AnyObject]
print(info)
}
And in another class I've:
NotificationCenter.default.post(name: NSNotification.Name.init("dbReady"), object: dictionary)
All of syntax for selector doesn't work
I tried:
1. fetchedUser
2. fetchedUser:
3. fetchedUser(notification:)
4. "fetchedUser:"
And other ten options maybe. Can anyone help me?
Upvotes: 0
Views: 748
Reputation: 285069
In Swift 3 the (system) notifications have this standard signature:
func notifFunction(_ notification: Notification)
So your function is supposed to be
func fetchedUser(_ notification: Notification){
And the corresponding selector is
#selector(fetchedUser(_:))
For convenience you could use an extension of Notification.Name
extension Notification.Name {
static let databaseReady = NSNotification.Name("dbReady")
}
Then you can write
NotificationCenter.default.addObserver(self, selector: #selector(fetchedUser(_:)) , name: .databaseReady, object: nil)
and
NotificationCenter.default.post(name: .databaseReady, object: dictionary)
Upvotes: 3
Reputation: 980
It works on my project.
Post Notification
let dic: [String:AnyObject] = ["news_id": 1 as AnyObject,"language_id" : 2 as AnyObject]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationKeyIndentifier"), object: dic)
Notification Observer
Add following code on required class where you want to observe.
NotificationCenter.default.addObserver(self, selector: #selector(self.handlePushNotification(notification:)), name: NSNotification.Name(rawValue: "NotificationKeyIndentifier"), object: nil)
This is the function, which is trigger after observed by the notification observer.
func handlePushNotification(notification: NSNotification){
if let dic = notification.object as? [String: AnyObject]{
if let language_id = dic["language_id"] as? Int{
if let news_id = dic["news_id"] as? Int{
print(language_id)
print(news_id)
}
}
}
}
Hope it may help you. If you have any problem regarding this please fill free to ask questions.
Upvotes: 1
Reputation: 21144
class Observer {
init() {
let name = NSNotification.Name("TestNotification")
NotificationCenter.default.addObserver(self, selector: #selector(notificationDidTrigger), name: name, object: nil)
}
@objc func notificationDidTrigger(notification: Notification) {
print("Notification triggered ", notification.userInfo)
}
}
let obj = Observer()
let name = NSNotification.Name("TestNotification")
var notification = Notification(name: name, object: nil)
notification.userInfo = ["Name": "My notification"]
NotificationCenter.default.post(notification)
Upvotes: 0
Reputation: 325
My error was a selector with name didn't exist "fetchedUserWithNotification:". I solved my problem by rewriting a new class and copy and paste all its content. Maybe it was a Xcode bug (IMHO the last version is plenty of it )
Upvotes: 0
Reputation: 969
You can try this:
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.fetchedUser), name: notificationName, object: nil)
Upvotes: 0