Instance Method インスタンスメソッド

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 observer is 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.

See Also 参照

Registering for Observation