Replace object at key path in NS[Mutable]Dictionary


Jonathan Taylor
 

HiI all,

I have a dictionary that has been loaded from a plist file, which has a deep structure of dictionaries within dictionaries. I would like to modify just one key-value pair deep in that hierarchy. Is there a neat off-the-shelf way of doing that?

I can load the plist file as an NSMutableDictionary, but it seems that only makes the top level dictionary mutable. The dictionaries-within-dictionaries are still immutable. Suppose I want to modify the value associated with the key path dict.next_level.deeper.even_deeper.key. The only way I can see to achieve this is to replicate “even_deeper" as a mutable dictionary, modify the value of “key” in it, replicate “deeper” and modify “even_deeper”, replicate “next_level” and modify “deeper”, and finally modify “next_level” within “dict”.

I could write recursive code to do that, but it feels as if there ought to be a more straightforward way of achieving what I want. Can anyone point me at something I have missed?

Cheers
Jonny.

[University of Glasgow: The Times Scottish University of the Year 2018]

Join cocoa@apple-dev.groups.io to automatically receive all group messages.