Instance Method

removeObserver(_:name:object:)

Removes matching entries from the notification center's dispatch table.

Declaration

func removeObserver(_ observer: Any, name aName: NSNotification.Name?, object anObject: Any?)

Parameters

observer

Observer to remove from the dispatch table. Specify an observer to remove only entries for this observer.

aName

Name of the notification to remove from dispatch table. Specify a notification name to remove only entries that specify this notification name. When nil, the receiver does not use notification names as criteria for removal.

anObject

Sender to remove from the dispatch table. Specify a notification sender to remove only entries that specify this sender. When nil, the receiver does not use notification senders as criteria for removal.

Discussion

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc method. Otherwise, you should call this method or removeObserver(_:) before observer or any object specified in addObserver(forName:object:queue:using:) or addObserver(_:selector:name:object:) is deallocated.

See Also

Adding and Removing Notification Observers

func addObserver(forName: NSNotification.Name?, object: Any?, queue: OperationQueue?, using: (Notification) -> Void) -> NSObjectProtocol

Adds an entry to the notification center's dispatch table that includes a notification queue and a block to add to the queue, and an optional notification name and sender.

func addObserver(Any, selector: Selector, name: NSNotification.Name?, object: Any?)

Adds an entry to the notification center's dispatch table with an observer and a notification selector, and an optional notification name and sender.

func removeObserver(Any)

Removes all entries specifying a given observer from the notification center's dispatch table.