<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NotificationCenter/addObserver(of:for:using:)-56bn4",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:So20NSNotificationCenterC10FoundationE11addObserver2of3for5usingAbCE16ObservationTokenV7SubjectQzSg_xmyxScMYcctAbCE16MainActorMessageRzAKRLClF"
  },
  "title" : "addObserver(of:for:using:)"
}
-->

# addObserver(of:for:using:)

Adds an observer to a center for messages delivered on the main actor with a given subject and message type.

```
func addObserver<Message>(of subject: Message.Subject? = nil, for messageType: Message.Type, using observer: @escaping @MainActor (Message) -> Void) -> NotificationCenter.ObservationToken where Message : NotificationCenter.MainActorMessage, Message.Subject : AnyObject
```

## Parameters

`subject`

The subject to be observed. Specify a metatype to observe all values for a given type.

`messageType`

The message type to be observed.

`observer`

A closure to execute when receving a message.

## Return Value

A token representing the observation registration with the given notification center.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)