Posting and Observing Notifications
Send and receive app-wide events.
Posting and Observing Notifications is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
NotificationCenter
NotificationCenter is a broadcast mechanism: one part of the app posts a notification and any number of observers react, without the sender and receivers knowing each other.
The Default Center
Most code uses NotificationCenter.default, a shared center that routes notifications across the process.
import Foundation
let center = NotificationCenter.default
print("Using the default notification center")Defining a Notification Name
Notifications are identified by a Notification.Name. Defining a constant avoids typos and centralizes the name.
import Foundation
extension Notification.Name {
static let dataUpdated = Notification.Name("dataUpdated")
}
print("Defined a notification name")Posting a Notification
post(name:object:) broadcasts the notification. The object is usually the sender or nil.
import Foundation
let center = NotificationCenter.default
let name = Notification.Name("ping")
center.post(name: name, object: nil)
print("Posted a notification")Adding an Observer (Closure)
The block-based addObserver(forName:object:queue:using:) runs a closure whenever the notification fires and returns a token.
import Foundation
let center = NotificationCenter.default
let name = Notification.Name("ping")
let token = center.addObserver(forName: name, object: nil, queue: .main) { _ in
print("Observer fired")
}
center.post(name: name, object: nil)
_ = tokenSelector-Based Observers
The classic API uses a target and #selector. The method receives a Notification argument.
import Foundation
class Watcher: NSObject {
func start() {
NotificationCenter.default.addObserver(
self, selector: #selector(handle(_:)),
name: Notification.Name("ping"), object: nil)
}
@objc func handle(_ n: Notification) { print("Handled") }
}
Watcher().start()Receiving the Notification
The observer closure or method receives a Notification value carrying its name, object, and userInfo.
import Foundation
let center = NotificationCenter.default
let name = Notification.Name("event")
_ = center.addObserver(forName: name, object: nil, queue: nil) { note in
print("Got: \(note.name.rawValue)")
}
center.post(name: name, object: nil)One-to-Many Broadcast
Many observers can listen for the same name. A single post notifies all of them.
import Foundation
let center = NotificationCenter.default
let name = Notification.Name("refresh")
_ = center.addObserver(forName: name, object: nil, queue: nil) { _ in print("A") }
_ = center.addObserver(forName: name, object: nil, queue: nil) { _ in print("B") }
center.post(name: name, object: nil)Filtering by Object
Passing a non-nil object to addObserver filters so the observer only fires for posts from that sender.
import Foundation
let center = NotificationCenter.default
let sender = NSObject()
let name = Notification.Name("scoped")
_ = center.addObserver(forName: name, object: sender, queue: nil) { _ in
print("Only from sender")
}
center.post(name: name, object: sender)System Notifications
The system posts many built-in notifications, such as keyboard or app-lifecycle events, which you observe the same way.
import Foundation
// e.g. UIApplication.didEnterBackgroundNotification on iOS
let custom = Notification.Name("appReady")
_ = NotificationCenter.default.addObserver(forName: custom, object: nil, queue: nil) { _ in
print("App ready")
}When to Use It
NotificationCenter suits global, one-to-many events where sender and receivers should stay decoupled. For tight one-to-one links a delegate is often clearer.
import Foundation
// Broadcast a global event many parts of the app care about.
let name = Notification.Name("userLoggedOut")
NotificationCenter.default.post(name: name, object: nil)
print("Broadcast logout")Quick Check
What does NotificationCenter.default.post(name:object:) do?
Recap
NotificationCenter.default decouples senders and receivers: you define a Notification.Name, post it, and addObserver with a closure or selector to react. It supports one-to-many broadcast and object filtering. Next: carrying data in userInfo.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Posting and Observing Notifications” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Posting and Observing Notifications”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “Posting and Observing Notifications”?
Send and receive app-wide events. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Posting and Observing Notifications” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Posting and Observing Notifications
- Notification userInfo Payloads
- Removing Observers Safely
- The Observer Pattern Alternatives