removeObserver(_:forKeyPath:context:)
Stops the observer object from receiving change notifications for the property specified by the key path relative to the object receiving this message, given the context.
Declaration
func removeObserver(_ observer: NSObject, forKeyPath keyPath: String, context: UnsafeMutableRawPointer?)Parameters
- observer:
The object to remove as an observer.
- keyPath:
A key-path, relative to the observed object, for which
observeris registered to receive KVO change notifications. - context:
Arbitrary data that more specifically identifies the observer to be removed.
Discussion
Examining the value in context you are able to determine precisely which addObserver(_:forKeyPath:options:context:) invocation was used to create the observation relationship. When the same observer is registered for the same key-path multiple times, but with different context pointers, an application can determine specifically which object to stop observing. It is an error to call removeObserver(_:forKeyPath:context:) if the object has not been registered as an observer.
Be sure to invoke this method (or removeObserver(_:forKeyPath:)) before any object specified in addObserver(_:forKeyPath:options:context:) is deallocated.