---
title: "addObserver(of:for:using:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/notificationcenter/addobserver(of:for:using:)-64uw3"
---

# addObserver(of:for:using:)

Adds an observer to a center for messages delivered asynchronously with a given subject and message type.

## Declaration

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

## Parameters

- `subject`: The subject to observe. 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

Return Value A token representing the observation registration with the given notification center.  Retain this token for as long as you need to receive messages.

## See Also

### Observing concurrency-safe notifications

- [addObserver(of:for:using:)](foundation/notificationcenter/addobserver(of:for:using:)-4d19x.md)
- [addObserver(of:for:using:)](foundation/notificationcenter/addobserver(of:for:using:)-90os.md)
- [addObserver(of:for:using:)](foundation/notificationcenter/addobserver(of:for:using:)-56bn4.md)
- [addObserver(of:for:using:)](foundation/notificationcenter/addobserver(of:for:using:)-twm3.md)
- [addObserver(of:for:using:)](foundation/notificationcenter/addobserver(of:for:using:)-t1wr.md)
- [removeObserver(_:)](foundation/notificationcenter/removeobserver(_:)-2gmm0.md)
- [NotificationCenter.ObservationToken](foundation/notificationcenter/observationtoken.md)
