-1

Given:

An existing NSMutableDictionary named oldDictionary.

Goal:

Copy oldDictionary to a another dictionary named newDictionary and if newDictionary has been modified – e.g., the values of some keys are changed – then oldDictionary should update to reflect the change as well.

Question:

What is the best way to do that?

Please advise me on this issue.

ravron
  • 11,014
  • 2
  • 39
  • 66
tranvutuan
  • 6,089
  • 8
  • 47
  • 83
  • 3
    Just use the same dictionary! – Sulthan Jul 26 '12 at 16:10
  • 1
    As Sulthan said, if you're applying the same actions then just use the same dictionary, otherwise for every action you perform you have to compare the two dictionaries and remove the changes. This isn't a lot of overhead for small things but if this was in a loop then you're looking at a lot of memory usage for something unnecessary – Elmo Jul 26 '12 at 16:16

1 Answers1

1

Not sure that's the best way, i believe you can assign the observers for every key-value of the old one with

addObserver:forKeyPath:options:context:

processing every change at

observeValueForKeyPath:ofObject:change:context:

Check the KVO reference for details. The reason of why that's not implemented as a standard functionality must be because that's an overkill.

A-Live
  • 8,904
  • 2
  • 39
  • 74