Skip to content

Instantly share code, notes, and snippets.

@dagronf
Last active April 13, 2024 12:50
Show Gist options
  • Save dagronf/fca14535894070cfabb052d9969d00ea to your computer and use it in GitHub Desktop.
Save dagronf/fca14535894070cfabb052d9969d00ea to your computer and use it in GitHub Desktop.
Observe all notifications sent through a NotificationCenter or DistributedNotificationCenter
// Observe all notifications generated by the default NotificationCenter
NotificationCenter.default.addObserver(
forName: nil, object: nil, queue: nil) { notification in
Swift.print("Notification: \(notification.name.rawValue), Object: \(notification.object)")
}
// Observe all notifications generated by the default DistributedNotificationCenter
DistributedNotificationCenter.default().addObserver(
forName: nil, object: nil, queue: nil) { notification in
Swift.print("Notification: \(notification.name.rawValue), Object: \(notification.object)")
}
@dagronf
Copy link
Author

dagronf commented Aug 10, 2021

Output looks like

Notification: NSApplicationWillUpdateNotification, Object: Optional(<NSApplication: 0x7fcf53c05320>)
Notification: NSWindowDidUpdateNotification, Object: Optional(<NSWindow: 0x7fcf53c062d0>)
Notification: NSApplicationDidUpdateNotification, Object: Optional(<NSApplication: 0x7fcf53c05320>)
Notification: NSAutomaticFocusRingChanged, Object: Optional(<NSTabView: 0x7fcf63d87160>)
Notification: NSApplicationWillUpdateNotification, Object: Optional(<NSApplication: 0x7fcf53c05320>)
Notification: NSWindowDidUpdateNotification, Object: Optional(<NSWindow: 0x7fcf53c062d0>)
Notification: NSApplicationDidUpdateNotification, Object: Optional(<NSApplication: 0x7fcf53c05320>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment