If I have an immutable NSDictionary with nested hierarchy, from a JSON string, what is the easiest way to change a value for a key that is deeply nested in the hierarchy?
For example, I have a dictionary, and the value for “key1” is an array, inside the array, each element is a dictionary, and inside each dictionary, there is a value for key “key2”, now I want to change the value for “key2”, since the whole data structure is immutable, which makes it difficult, should I duplicate this data structure with mutable collection so that I can change that value, this seems to have a lot of overhead, but this is the only way that came into my mind.
i’m not totally sure, but i think you’ll have to pass everyone of your dictionary and array to a mutable one.
It depends if you want to still have a immutable structure after the change or if it doesn’t matter if it’s still mutable after it.
if you want to stay immutable after the change, then you’ll have to use temp variable for mutable dict and array.
if it doesn’t matter for you if it’s mutable, then you’d have to make it mutable when retrieving it from the JSON by using a temp immutable structure and make it mutable permanently.
i hope it’ll be helpfull to you.