0Pricing
Swift Academy · Lesson

The Observer Pattern Alternatives

Compare with delegates and Combine publishers.

Observer Pattern Alternatives

NotificationCenter is one way to react to events. Swift offers others: delegation, Combine, and async streams. Each fits a different shape of communication.

The Observer Pattern

The observer pattern lets objects subscribe to events from a subject. NotificationCenter is a global, loosely coupled implementation of it.

import Foundation

// Subject posts, observers react, neither holds the other.
NotificationCenter.default.post(name: Notification.Name("tick"), object: nil)
print("Broadcast an event")

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