Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 794
Voloda2 Avatar asked Aug 13 '11 10:08

Voloda2


3 Answers

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.
like image 147
PeyloW Avatar answered Nov 16 '22 04:11

PeyloW


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];
like image 23
noobular Avatar answered Nov 16 '22 02:11

noobular


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.

like image 1
Ole Begemann Avatar answered Nov 16 '22 04:11

Ole Begemann