Voloda2
Voloda2

Reputation: 12607

What is the difference between NSKeyValueObservingOptionNew and NSKeyValueObservingOptionOld?

NSKeyValueObservingOptionOld
    Indicates that the change dictionary should contain the old attribute value, if applicable.

What does it mean old attribute value?

Upvotes: 8

Views: 6166

Answers (3)

noobular
noobular

Reputation: 3287

You can do something like:

[self addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

and pick up the values via:

CGSize newSize = [[change objectForKey:@"new"] CGSizeValue];
CGSize oldSize = [[change objectForKey:@"old"] CGSizeValue];

Upvotes: 2

PeyloW
PeyloW

Reputation: 36752

The observer is notified when an observed key path changes it's value. The ´change` dictionary contains information related to how the observed key path has changed. This dictionary is only filled with the values according to the options that you provide when setting

  • NSKeyValueObservingOptionNew - Specifies that you want to have access to the new value that the key path changed into.
  • NSKeyValueObservingOptionOld - Specifies that you want to have access to the old value that the key path changed from.

If specified to be sent these old and/or new values are accessible from the change dictionary using these keys:

  • NSKeyValueChangeNewKey - To access the new value.
  • NSKeyValueChangeOldKey - To access the old/previous value.

Upvotes: 13

Ole Begemann
Ole Begemann

Reputation: 135548

It means the dictionary that is an argument of observeValueForKeyPath:ofObject:change:context: contains a key-value pair that tells you the old value of the observed property.

Upvotes: 1

Related Questions