<!--
{
  "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/MainActorMessage",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:So20NSNotificationCenterC10FoundationE16MainActorMessageP"
  },
  "title" : "NotificationCenter.MainActorMessage"
}
-->

# NotificationCenter.MainActorMessage

A protocol for creating types that you can post to a notification center and bind to the main actor.

```
protocol MainActorMessage : SendableMetatype
```

## Overview

You post types conforming to  `MainActorMessage` to a notification center using `post(_:subject:)` and observe them with `addObserver(of:for:using:)`. The notification center delivers `MainActorMessage` types synchronously when posted.

For types that post on an arbitrary isolation, use [`NotificationCenter.AsyncMessage`](/documentation/Foundation/NotificationCenter/AsyncMessage).

Each `MainActorMessage` is associated with a specific `Subject` type.

For example, a `MainActorMessage` associated with the type `Event` could use the following declaration:

```swift
struct EventDidStart: NotificationCenter.MainActorMessage {
    typealias Subject = Event
}
```

`MainActorMessage` can use an optional [`NotificationCenter.MessageIdentifier`](/documentation/Foundation/NotificationCenter/MessageIdentifier) type for context-aware observer registration:

```swift
extension NotificationCenter.MessageIdentifier where Self == NotificationCenter.BaseMessageIdentifier<EventDidStart> {
    static var didStart: Self { .init() }
}
```

With this identifier, observers can receive information about a specific instance by registering for this message with a [`NotificationCenter`](/documentation/Foundation/NotificationCenter):

```swift
let observerToken = NotificationCenter.default.addObserver(of: importantEvent, for: .didStart)
```

Or an observer can receive information about any instance with:

```swift
let observerToken = NotificationCenter.default.addObserver(of: Event.self, for: .didStart)
```

The notification center ties observation the lifetime of the returned [`NotificationCenter.ObservationToken`](/documentation/Foundation/NotificationCenter/ObservationToken) and automatically de-registers the observer if the token
goes out of scope. You can also remove observation explicitly:

```swift
NotificationCenter.default.removeObserver(observerToken)
```

### Notification Interoperability

`MainActorMessage` includes optional interoperability with [`Notification`](/documentation/Foundation/Notification), enabling posters and observers of both types
to pass information.

It does this by offering a [`makeMessage(_:)`](/documentation/Foundation/NotificationCenter/MainActorMessage/makeMessage(_:)) method that collects values from a [`Notification`](/documentation/Foundation/Notification)‘s [`userInfo`](/documentation/Foundation/Notification/userInfo) and populates properties on a new message.
In the other direction, a [`makeNotification(_:)`](/documentation/Foundation/NotificationCenter/MainActorMessage/makeNotification(_:)) method collects the message’s defined properties and loads them into a new notification’s [`userInfo`](/documentation/Foundation/Notification/userInfo) dictionary.

For example, if there exists a [`Notification`](/documentation/Foundation/Notification) posted on `MainActor` identified by the [`Notification.Name`](/documentation/Foundation/Notification/Name-swift.typealias) `"eventDidFinish"` with a [`userInfo`](/documentation/Foundation/Notification/userInfo)
dictionary containing the key `"duration"` as an [`NSNumber`](/documentation/Foundation/NSNumber), an app could post and observe the notification with the following [`NotificationCenter.MainActorMessage`](/documentation/Foundation/NotificationCenter/MainActorMessage):

```swift
struct EventDidFinish: NotificationCenter.MainActorMessage {
    typealias Subject = Event
    static var name: Notification.Name { Notification.Name("eventDidFinish") }

    var duration: Int

    static func makeNotification(_ message: Self) -> Notification {
        return Notification(name: Self.name, userInfo: ["duration": NSNumber(message.duration)])
    }

    static func makeMessage(_ notification: Notification) -> Self? {
        guard let userInfo = notification.userInfo,
              let duration = userInfo["duration"] as? Int
        else {
            return nil
        }

        return Self(duration: duration)
    }
}
```

With this definition, an observer for this `MainActorMessage` type receives information even if the poster used the [`Notification`](/documentation/Foundation/Notification) equivalent, and vice versa.

## Topics

### Declaring the message name and subject

[`name`](/documentation/Foundation/NotificationCenter/MainActorMessage/name)

A optional name corresponding to this type, used to interoperate with notification posters and observers.

[`Subject`](/documentation/Foundation/NotificationCenter/MainActorMessage/Subject)

A type which you can optionally post and observe along with this `MainActorMessage`.

### Converting between messages and notifications

[`makeMessage(_:)`](/documentation/Foundation/NotificationCenter/MainActorMessage/makeMessage(_:))

Converts a posted notification into this main actor message type for any observers.

[`makeNotification(_:)`](/documentation/Foundation/NotificationCenter/MainActorMessage/makeNotification(_:))

Converts a posted main actor message into a notification for any observers.

## Relationships

### Conforming Types

[`UbiquityIdentityDidChangeMessage`](/documentation/Foundation/FileManager/UbiquityIdentityDidChangeMessage)

[`DidCloseUndoGroupMessage`](/documentation/Foundation/UndoManager/DidCloseUndoGroupMessage)

[`WillCloseUndoGroupMessage`](/documentation/Foundation/UndoManager/WillCloseUndoGroupMessage)

[`WillRedoChangeMessage`](/documentation/Foundation/UndoManager/WillRedoChangeMessage)

[`DidEnterBackgroundMessage`](/documentation/Foundation/NSExtensionContext/DidEnterBackgroundMessage)

[`DidOpenUndoGroupMessage`](/documentation/Foundation/UndoManager/DidOpenUndoGroupMessage)

[`DidUndoChangeMessage`](/documentation/Foundation/UndoManager/DidUndoChangeMessage)

[`SystemTimeZoneDidChangeMessage`](/documentation/Foundation/TimeZone/SystemTimeZoneDidChangeMessage)

[`SizeLimitExceededMessage`](/documentation/Foundation/UserDefaults/SizeLimitExceededMessage)

[`CheckpointMessage`](/documentation/Foundation/UndoManager/CheckpointMessage)

[`CurrentLocaleDidChangeMessage`](/documentation/Foundation/Locale/CurrentLocaleDidChangeMessage)

[`DidBecomeActiveMessage`](/documentation/Foundation/NSExtensionContext/DidBecomeActiveMessage)

[`WillResignActiveMessage`](/documentation/Foundation/NSExtensionContext/WillResignActiveMessage)

[`DidRedoChangeMessage`](/documentation/Foundation/UndoManager/DidRedoChangeMessage)

[`SystemClockDidChangeMessage`](/documentation/Foundation/Date/SystemClockDidChangeMessage)

[`WillUndoChangeMessage`](/documentation/Foundation/UndoManager/WillUndoChangeMessage)

[`WillEnterForegroundMessage`](/documentation/Foundation/NSExtensionContext/WillEnterForegroundMessage)

### Inherits From

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

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)