Notification userInfo Payloads
Attach typed data to notifications.
Notification userInfo
A notification can carry data in its userInfo dictionary, letting observers receive details about what happened.
Posting with userInfo
Pass a dictionary to post(name:object:userInfo:). Keys and values are Any, so any payload fits.
import Foundation
let center = NotificationCenter.default
let name = Notification.Name("scoreChanged")
center.post(name: name, object: nil, userInfo: ["score": 100])
print("Posted with payload")All lessons in this course
- Posting and Observing Notifications
- Notification userInfo Payloads
- Removing Observers Safely
- The Observer Pattern Alternatives