How do you create custom notifications in Swift 3?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift 3, “custom notifications” usually means custom app-internal notifications posted through NotificationCenter. This is a way for one part of the app to broadcast an event without directly depending on every listener.
That makes notifications useful for loose coupling, but they are still just one tool. The basic flow is: define a notification name, post it, and observe it somewhere else.
Define a Notification Name
The first step is to avoid raw strings spread across the codebase. In Swift 3, the common pattern is an extension on Notification.Name.
This gives you one central constant instead of several string literals that can drift out of sync.
Post the Notification
Once the name exists, any object can post it through NotificationCenter.default.
This notification carries:
- a name,
- an optional sender object,
- and optional
userInfodata.
That is the whole payload model for a basic custom notification.
Observe the Notification
In Swift 3, one of the most common observation styles uses selector-based observers.
This is the classic Swift 3 pattern. The observer registers in viewDidLoad and unregisters in deinit.
Filtering by Sender
If you want to listen only to notifications from a specific object, pass that object into the observer call instead of nil.
That can reduce accidental cross-talk in larger apps where the same event name may be used by several instances.
When userInfo Is Useful
userInfo is the simplest way to attach small bits of context to the event.
Example:
Then read it in the handler:
Use keys consistently. If the event payload becomes large or complicated, that is often a sign the app may need a stronger abstraction than raw notifications.
When Notifications Are a Good Fit
Custom notifications work well when:
- multiple parts of the app may care about the same event,
- the sender should not know all receivers,
- and the payload is small and event-like.
They are less ideal when:
- one object has exactly one clear delegate,
- the data contract is large or strongly typed,
- or the interaction is really a direct request rather than a broadcast event.
In those cases, delegates, closures, or dedicated service calls are often cleaner.
Threading and UI Updates
NotificationCenter does not automatically move your code onto the main thread. If the notification leads to UI work, make sure the observer handles that correctly.
For example:
That matters when notifications are posted from background work such as networking or file operations.
Common Pitfalls
One common mistake is using raw string names everywhere instead of centralizing them in a Notification.Name extension. That creates typo-prone code.
Another mistake is forgetting to remove observers in older selector-based patterns. That can lead to crashes or confusing behavior when objects outlive their intended lifecycle.
It is also easy to overuse notifications for everything. They are useful for broadcast events, but they make code harder to trace when used as a replacement for all direct communication.
Finally, userInfo is only a dictionary. If the payload contract is important, document the keys clearly and keep the data small and predictable.
Summary
- In Swift 3, custom app notifications are usually created with
NotificationCenter. - Define names in a
Notification.Nameextension instead of using raw strings. - Post notifications with an optional sender object and
userInfodictionary. - Observe them with
addObserverand a selector, then remove observers when appropriate. - Use notifications for loosely coupled broadcast events, not for every kind of object communication.
Related reading
- How do you debug React Native?
- How do you debug React Native?
- How do you dismiss the keyboard when editing a UITextField
- How do you display a Toast from a background thread on Android?
- How do you document the parameters of a function's closure parameter in Swift 3?
- How do you draw a line programmatically from a view controller?
- How do you dynamically add elements to a ListView on Android?
- How do you find out the type of an object in Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.