SundayMonday
SundayMonday

Reputation: 19737

Objective-c KVO: object vs changeDictionary

I'm using KVO in an iOS app to notify a viewController when a property on an object changes. When I addObserver I specify options:NSKeyValueObservingOptionNew.

In observeValueForKeyPath I can get the value of the property in two ways: from the object that's passed to observeValueForKeyPath. Or from the NSDictionary that's passed to the same method.

Will these two approaches to getting the value of the updated property always return the same result?

Recall I'm specifying NSKeyValueObservingOptionNew as the options when I addObserver. From Apple's docs it's sounds like specifying NSKeyValueObservingOptionInitial could result in a difference.

Upvotes: 1

Views: 326

Answers (1)

ipmcc
ipmcc

Reputation: 29906

In the common case, yes, these objects should be the same, although it would be possible to contrive a scenario where they weren't (i.e. a custom getter that generates a new value on every call, or something like that.) NSKeyValueObservingOptionInitial won't have any bearing on the contents of that dictionary, it just determines whether or not the observer is notified immediately vs. waiting for the first value change after adding the observer. This is useful if you need some state in the observer to be "primed."

Upvotes: 1

Related Questions