What is the difference between NSKeyValueObservingOptionNew and NSKeyValueObservingOptionOld?

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

What is the meaning of the old attribute?

+7
source share
3 answers

The observer is notified when the observed key path changes its value. The "change" dictionary contains information on how the observed key path has changed. This dictionary is filled only with values ​​in accordance with the parameters that you provide when setting up

  • NSKeyValueObservingOptionNew - indicates that you want to access the new value with which the key path was changed.
  • NSKeyValueObservingOptionOld - indicates that you want to access the old value from which the key path was changed.

If indicated for sending, these old and / or new values ​​are accessible from the change dictionary using the following keys:

  • NSKeyValueChangeNewKey - to access the new value.
  • NSKeyValueChangeOldKey - to access the old / previous value.
+12
source

You can do something like:

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

and select values ​​via:

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

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

+1
source

All Articles