特定のプロパティの値の変更を監視できる。
addObserver:forKeyPath:options:context:
Registers anObserver to receive KVO notifications for the specified key-path relative to the receiver.
func addObserver(_ observer: NSObject,
forKeyPath keyPath: String,
options options: NSKeyValueObservingOptions,
context context: UnsafeMutablePointer<Void>)
Parameters
anObserver
The object to register for KVO notifications. The observer must implement the key-value observing method
observeValueForKeyPath:ofObject:change:context:
.
KVO通知にオブザーザとして登録されるオブジェクト。このオブジェクトはKVOメソッドの observeValueForKeyPath:ofObject:change:context:
を実装してなければならない。
keyPath
The key path, relative to the receiver, of the property to observe. This value must not be nil.
監視するプロパティの key path です。必須です。
(key path はプロパティ名を文字列で指定する)
options
A combination of the NSKeyValueObservingOptions values that specifies what is included in observation notifications. For possible values, see NSKeyValueObservingOptions.
監視したい通知を含んだ NSKeyValueObservingOptions
の組合せです。 NSKeyValueObservingOptions
を指定します。
context
Arbitrary data that is passed to anObserver in observeValueForKeyPath:ofObject:change:context:.
observeValueForKeyPath:ofObject:change:context:
に送られる仲裁データです。
Discussion
Neither the receiver, nor anObserver, are retained. An object that calls this method must also call either the removeObserver:forKeyPath: or removeObserver:forKeyPath:context: method when participating in KVO.
レシーバもオブザーバも保持(retain)されません。KVOを利用してこのメソッドを呼び出すオブジェクトは removeObserver:forKeyPath:
または removeObserver:forKeyPath:context:
も呼び出す必要が有ります。