How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
NotificationCenter lets one part of an iOS app broadcast an event without knowing which other parts are listening. To pass data with that event, you attach a userInfo dictionary. Swift 3 changed the API names compared with Swift 2, but the underlying model stayed the same: post a notification, observe it elsewhere, and extract typed values from the payload carefully.
Post a Notification With Data
In Swift 3 and later, define a notification name once and send the payload in userInfo.
The dictionary is flexible, which is useful, but it also means the sender and receiver must agree on key names and value types.
Observe and Read the Payload
The receiver extracts values from notification.userInfo.
Because userInfo is loosely typed, defensive casting is part of the pattern.
Swift 2 Used NSNotificationCenter
Swift 2 used older Foundation naming, but the idea was identical.
Observation looked similar, just with older method names:
So the migration issue is mostly API naming, not a different communication technique.
Block-Based Observation
For some cases, the closure-based API is cleaner than selector-based observation.
If you use this form, store the token and remove it when appropriate. Otherwise the observer can outlive the object that created it.
When NotificationCenter Is the Right Tool
Notifications are best for broadcast-style events such as:
- login state changed
- theme changed
- sync completed
- cache refreshed
They are not ideal for tightly coupled one-to-one communication. If one object already knows exactly which other object needs the data, a delegate, closure, or direct method call is usually clearer.
Keep the Payload Contract Stable
As the app grows, ad hoc string keys become fragile. Shared key constants or a small wrapper around the payload contract help prevent silent failures caused by typos.
That is especially useful when several modules post or observe the same notification.
Also remember that notification handlers may not always arrive on the main thread unless you request a main queue or dispatch explicitly. That matters for any observer that updates UIKit state.
Common Pitfalls
- Using notifications for tightly coupled communication that should be direct.
- Passing inconsistent or poorly documented
userInfokeys. - Assuming payload values are type-safe without casting them carefully.
- Forgetting to remove long-lived observers or stored block tokens.
Summary
- Pass data through notifications with the
userInfodictionary. - In Swift 3 and later, prefer typed
Notification.Nameconstants. - Swift 2 used
NSNotificationCenter, but the data-passing model was the same. - Cast payload values defensively and document the key contract.
- Use notifications for broadcast events, not as a universal data-passing mechanism.

