Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could create an NSMutableDictionary and loop through your array, adding the keys to the mutable dictionary using the <code>allKeys</code>.</p> <p>For example, if your array was called <code>dictArray</code>, you could do:</p> <pre><code>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]]; } } </code></pre> <p>This code first creates a dictionary <code>combinedDictionary</code> that will be your final dictionary. It loops through all of the dictionaries in your array and for each one does the following:</p> <p>First, it gets an array of all keys in the dictionary. For the dictionaries you provided this array will look like <code>@[@"Bath"]</code> for the first 3 and <code>@[@"Birmingam"]</code> for the other two.</p> <p>The code then loops through these keys and gets the already existing dictionary from the combined dictionary from this key. If the dictionary doesn't exist, one is created.</p> <p>Then, it adds all of the values from the dictionary from the array and sets the new dictionary to be the one in <code>combinedDictionary</code>.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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