Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd Mutable Dictionary to a Mutable Dictionary for conversion to JSON
    primarykey
    data
    text
    <p>I am trying to build a dictionary which has as dictionary within it (eventually I hope to convert to a JSON). The problem is I am having problems building it.</p> <p>So far I have this, what it should do is build a small dictionary with keys and add it to a larger dictionary, reset and then load the small dict and then add it to the large one.</p> <pre><code>NSMutableDictionary *nestedList = [[NSMutableDictionary alloc]init]; NSMutableDictionary *nestedSections = [[NSMutableDictionary alloc] init]; [nestedList addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:46], @"menuHeight", @"editText", @"menuMethod", [NSNumber numberWithInt:1], @"menuOption", nil]]; [nestedSections addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys: nestedList, "@Basic", nil]]; [nestedList removeAllObjects]; [nestedList addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:92], @"menuHeight", @"sendText", @"menuMethod", [NSNumber numberWithInt:1], @"menuOption", nil]]; [nestedSections addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys: nestedList, "@Pro", nil]]; </code></pre> <p>I then hoped to address like so;</p> <pre><code>NSString *string = [[nestedSections objectForKey:@"Pro"] objectForKey:@"menuMethod"]; NSLog(@"Method is : %@", string); </code></pre> <p>Log would hope to read <strong>sendText</strong></p> <p>The first dictionary builds fine but as soon as I try and add it to the second t bums out with EXC_BAD_ACCESS</p> <p>I think it is a memory addressing problem because they are both mutable but I am not sure, maybe nestedList should not be mutable. Any help appreciated.</p> <p>Ultimately I would like to convert this to a JSON like;</p> <pre><code>{ "Basic": { "menuHeight":"46", "menuMethod":"editText", "menuOption":"1", }, "Pro": { "menuHeight":"96", "menuMethod":"sendText", "menuOption":"1", } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload