Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Swift Dictionary

  1. Home
  2. Swift Dictionary
  3. NotificationCenter

NotificationCenter

Swift's NotificationCenter is a pub/sub mechanism for broadcasting events between objects. It allows communication between components that have no direct reference to each other.

Method List

MethodDescription
NotificationCenter.defaultThe shared default NotificationCenter instance.
post(name:object:userInfo:)Posts a notification.
addObserver(_:selector:name:object:)Registers an observer to receive notifications (Objective-C style).
addObserver(forName:object:queue:using:)Registers an observer to receive notifications using a closure.
removeObserver(_:)Removes a registered observer.
Notification.NameA structure that defines a notification identifier.

Sample Code

import Foundation

// Define custom notification names
extension Notification.Name {
    static let userDidLogin   = Notification.Name("userDidLogin")
    static let dataDidRefresh = Notification.Name("dataDidRefresh")
}

// Receive notifications using a closure
let observer1 = NotificationCenter.default.addObserver(
    forName: .userDidLogin,
    object: nil,
    queue: .main
) { notification in
    let userName = notification.userInfo?["name"] as? String ?? "Unknown"
    print("Login notification received: \(userName)")
}

let observer2 = NotificationCenter.default.addObserver(
    forName: .dataDidRefresh,
    object: nil,
    queue: .main
) { _ in
    print("Data refresh notification received")
}

// Post a notification (you can attach data via userInfo)
NotificationCenter.default.post(
    name: .userDidLogin,
    object: nil,
    userInfo: ["name": "Alice", "timestamp": Date()]
)

NotificationCenter.default.post(name: .dataDidRefresh, object: nil)

// Post multiple notifications
NotificationCenter.default.post(
    name: .userDidLogin,
    object: nil,
    userInfo: ["name": "Bob"]
)

// Remove observers (always remove to prevent memory leaks)
NotificationCenter.default.removeObserver(observer1)
NotificationCenter.default.removeObserver(observer2)

// No notifications are received after removal
NotificationCenter.default.post(
    name: .userDidLogin,
    object: nil,
    userInfo: ["name": "Carol"]
)
print("No notifications are received after removal")

// Example: receiving a system notification
let appObserver = NotificationCenter.default.addObserver(
    forName: NSNotification.Name("NSApplicationDidBecomeActiveNotification"),
    object: nil,
    queue: .main
) { _ in
    print("The app became active")
}

Notes

NotificationCenter is well suited for loosely coupled communication between screens and modules. The sender and receiver do not need to know about each other.

When using addObserver(forName:object:queue:using:), always store the returned observer token in a variable. Failing to remove an observer can cause memory leaks or unintended notification delivery. Always call removeObserver in deinit or when the view disappears.

For timer-based processing with Timer, see Timer.

If you find any errors or copyright issues, please .