0Pricing
Swift Academy · Lesson

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

  1. Posting and Observing Notifications
  2. Notification userInfo Payloads
  3. Removing Observers Safely
  4. The Observer Pattern Alternatives
← Back to Swift Academy