Instance MethodFoundationEssentials
addObserver(of:for:using:)
iOS 26.0+
iPadOS 26.0+
Mac Catalyst 26.0+
macOS 26.0+
tvOS 26.0+
visionOS 26.0+
watchOS 26.0+
Adds an observer to a center for messages delivered on the main actor with a given subject and identifier.
func addObserver<Identifier, Message>(of subject: Message.Subject, for identifier: Identifier, using observer: @escaping @MainActor (Message) -> Void) -> NotificationCenter.ObservationToken where Identifier : NotificationCenter.MessageIdentifier, Message : NotificationCenter.MainActorMessage, Message == Identifier.MessageType, Message.Subject : AnyObject
Parameters
subjectThe subject to observe. Specify a metatype to observe all values for a given type.
identifierAn identifier representing a specific message type.
observerA closure to execute when receving a message.
Return Value
A token representing the observation registration with the given notification center.