Note that there are some explanatory texts on larger screens.

plurals
  1. POIos NSDictionary array - grouping values and keys
    text
    copied!<p>I have the following result of <code>NSDictionary</code> of Array</p> <pre><code>Bath = { Keynsham = ( "nsham companies" ); }; Bath = { "Midsomer Norton" = ( "Keynsham companies" ); }; Bath = { "Norton Radstock" = ( "Keynsham taxi companies" ); }; Birmingham = { "Acock's Green" = ( "Acock's Green taxi companies" ); }; Birmingham = { "Alcester Lane's End" = ( "Alcester Lane's End taxi companies" ); }; </code></pre> <p>How can i combine the values and keys so that I end up with only one category as shown below;</p> <pre><code>Bath = { "Norton Radstock" = ( "Keynsham taxi companies" ); "Midsomer Norton" = ( "Keynsham companies" ); Keynsham = ( "nsham companies" ); }; </code></pre> <p>I am not sure if this is the best way to explain it the code is as follows</p> <p>//all Nssarrays allocated/ initialised</p> <pre><code> NSURL *url=[NSURL URLWithString:@"http://y.php"]; NSData *data= [NSData dataWithContentsOfURL:url]; NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:Nil]; //instantiate arrays to hold data NSMutableDictionary *dictArray=[[NSMutableDictionary alloc]init]; NSArray *cityName=[[NSArray alloc]init]; NSArray *townName=[[NSArray alloc]init]; NSArray *taxis=[[NSArray alloc]init]; NSArray *ids=[[NSArray alloc]init]; for (int i=0; i&lt;json.count; i++) { //cityName=[[NSMutableArray alloc] initWithCapacity:json.count]; ids = [[json objectAtIndex:i] objectForKey:@"id"]; cityName = [[json objectAtIndex:i] objectForKey:@"cityName"]; townName=[[json objectAtIndex:i] objectForKey:@"townName"]; taxis=[[json objectAtIndex:i] objectForKey:@"taxis"]; NSMutableArray *taxisArray=[[NSMutableArray alloc] initWithObjects:taxis,nil]; NSMutableDictionary *towensdict=[[ NSMutableDictionary alloc] initWithObjectsAndKeys:taxisArray,townName, nil]; NSMutableDictionary *cities1=[[NSMutableDictionary alloc] initWithObjectsAndKeys:towensdict,cityName, nil]; NSLOG (@"%@", cities1) here, gives me the print out above [dictArray addEntriesFromDictionary:cities1 ]; Then I tried Jdodgers solution as follows; NSMutableDictionary *combinedDictionary = [[NSMutableDictionary alloc] init]; for (NSDictionary *currentDictionary in dictArray) { NSArray *keys = [currentDictionary allKeys]; for (int n=0;n&lt;[keys count];n++) { NSMutableDictionary *dictionaryToAdd = [combinedDictionary valueForKey:[keys objectAtIndex:n]]; if (!dictionaryToAdd) dictionaryToAdd = [[NSMutableDictionary alloc] init]; [dictionaryToAdd setValuesForKeysWithDictionary:[currentDictionary valueForKey:[keys objectAtIndex:n]]]; [combinedDictionary setValue:dictionaryToAdd forKey:[keys objectAtIndex:n]]; NSLog(@"%@", currentDictionary); } } </code></pre> <p>//this gives error "unrecognized selector sent to instance", here is the print out</p> <pre><code>combinedDictionary NSMutableDictionary * 0x000000010012e580 currentDictionary NSDictionary *const 0x0000000100116460 dictArray NSMutableDictionary * 0x000000010012e220 [0] key/value pair key id 0x0000000100116460 [0] id value id 0x000000010012e440 [0] id keys NSArray * 0x0000000000000000 </code></pre>
 

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