0

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?

Forge
  • 6,538
  • 6
  • 44
  • 64
ndPPPhz
  • 315
  • 1
  • 15

5 Answers5

3

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)
Forge
  • 6,538
  • 6
  • 44
  • 64
vadian
  • 274,689
  • 30
  • 353
  • 361
  • 1
    Are you sure about the mandatory underscore? Then why does this http://stackoverflow.com/a/39352319/1187415 work? Or am I overlooking something? – Martin R Dec 01 '16 at 10:38
  • I'm not sure, but all system notifications have this signature (probably due to ObjC compatibility) – vadian Dec 01 '16 at 10:40
1

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.

Forge
  • 6,538
  • 6
  • 44
  • 64
Amrit Tiwari
  • 922
  • 7
  • 21
0

You can try this:

NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.fetchedUser), name: notificationName, object: nil)
Forge
  • 6,538
  • 6
  • 44
  • 64
Ram
  • 961
  • 6
  • 14
0
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)
Forge
  • 6,538
  • 6
  • 44
  • 64
Sandeep
  • 20,908
  • 7
  • 66
  • 106
0

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 )

Forge
  • 6,538
  • 6
  • 44
  • 64
ndPPPhz
  • 315
  • 1
  • 15