NSNotifications with userInfo in Swift 2.2

| | 0 Comments| 9:14 AM
Categories:

Xcode 7.3 Swift 2.2

Looking for help using notifications with Swift 3.0? Head on over here where we go over the latest Swift 3.0 changes.

When I sit down to blog I never know what will be a popular topic and what won’t. One that definitely surprised me was how popular the original NSNotifications with userInfo in Swift would end up being. Much has changed since Swift first hit the scenes, so I thought we’d provide an update to handling NSNotification userInfo and some working code to help out.

The technique for obtaining the default NSNotificationCenter has remained unchanged, and can be done with let nc = NSNotificationCenter.defaultCenter(). What has changed in the latest versions of Swift is how to specify the selector (i.e., the function that should be called when a notification has posted). Rather than using a bare string like "catchNotification", Xcode will instruct you to use the #selector directive, like this:

In this example we’re instructing the notification center to deliver MyNotification notifications to the catchNotification function of the ViewController class.

Post It!

Now, let’s look at posting (sending) a notification:

The userInfo still takes [NSObject : AnyObject]? as an argument, which we provide as a dictionary literal in Swift. Note that the userInfo values don’t need to be homogeneous (that’s where the AnyObject comes in); we are sending along a String and an NSDate.

Handling Notifications

The guard construct did not exist in Swift when I wrote the original code used to pull apart the userInfo data, but it serves as a good method to unwrap and verify that the expected data is in the userInfo.

To verify that the guard works properly switch out the NSDate() in the call to postNotificationName with a String or some other object. You should see No userInfo found in notification printed to the console.

Example Source

You can try out the code above with a simple iOS project. Create a new Single View Application and replace the contents of ViewController.swift with the following:

1 thought on “NSNotifications with userInfo in Swift 2.2”

  1. Thanks for this Joe. I was having some serious issues trying to properly handle receiving remote notifications and taking the user to a specific area within the application once they tap them. This tutorial has made for a MUCH simpler implementation of what I was trying to do.

    Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *