Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8874641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:46:23+00:00 2026-06-14T18:46:23+00:00

If I have an immutable NSDictionary with nested hierarchy, from a JSON string, what

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:46:24+00:00Added an answer on June 14, 2026 at 6:46 pm

    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.

    NSMutableDictionary *rootDict = [NSMutableDictionary dictionaryWithDictionary:rootImmutableDict];
    
    NSMutableArray* mutableArray = [NSMutableArray arrayWithArray:[rootImmutableDict objectForKey:@"key1"]];
    
    NSMutableDictionary* valueDict = [NSMutableDictionart dictionaryWithDictionary:[mutableArray objectAtIndex:idx]];
    
    [valueDict setObject:newValueObject forKey:@"key2"];
    
    [rootImmutableDict release];
    rootImmutableDict = nil;
    rootImmutableDict = [[NSDictionary alloc] initWithDictionary:rootDict];
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an class defining an immutable value type that I now need to
I have heard and read that a string can not be changed (immutable?). That
Say that I have an immutable Point class with x and y parameters, and
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I know that immutable objects always have the same state, the state in which
I have come across a class that has an immutable property: MyObject[] allObjs The
I have a property list which I read into an NSDictionary, from which I
I have an object factory that hands out instances of a few constant, immutable
I have an abstract immutable base class that defines enforces child classes to be
When I have an immutable parent class A that is NOT final, and another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.