Instance Method

removeObserver(_:)

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

Declaration

func removeObserver(_ observer: Any)

Parameters

observer

The observer to remove.

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(_:name:object:) before observer or any object specified in addObserver(forName:object:queue:using:) or addObserver(_:selector:name:object:) is deallocated.

You shouldn't use this method to remove all observers from a long-lived object, because your code may not be the only code adding observers that involve the object.

The following example illustrates how to unregister someObserver for all notifications for which it had previously registered. This is safe to do in the dealloc method, but should not otherwise be used (use removeObserver(_:name:object:) instead).

NotificationCenter.default.removeObserver(someObserver)

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, name: NSNotification.Name?, object: Any?)

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