11

I have a for loop that goes through a series of dictionaries in an array.

How can I consolidate all the dictionaries entries as it goes through the for loop into one NSMutableDictionary?

I tried addEntriesFromDictionary, but its not working. Thanks for your help.

for (int i=0; i<sections.count; i++){

    formElements    = [[sections objectAtIndex:i]objectForKey:@"Dictionary"];        
}
ThomasCle
  • 6,792
  • 7
  • 41
  • 81
user984248
  • 149
  • 1
  • 1
  • 8

4 Answers4

15

you can add dictionary object like below.

NSMutableDictionary *mDict=[NSMutableDictionary dictionary];
    [mDict addEntriesFromDictionary:DictObj];
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
  • What is happening now is everytime I go through the loop, the last dictionary replaces the one before it rather than adding to it. So I have 5 entries in the first dictionary, and 2 entries in the second dictionary. Ultimately, I want to combine them to have a count of 7 entries. – user984248 Aug 27 '12 at 13:41
  • @user984248 Yes because the keys replaces each other. You should probably use an array for this and just do [array addObject:formElements] – Jonathan Aug 27 '12 at 13:43
  • Can I do this and have a dictionary as the final result? I have all my code configured to read entries from a dictionary. – user984248 Aug 27 '12 at 13:46
  • You can't have ONE dictionary. As I assume the dictionary in `sections` has the same keys you need a unique key for each dictionary or create a multidimensional dictionary. – Jonathan Aug 27 '12 at 13:48
  • The dictionaries in sections have different keys – user984248 Aug 27 '12 at 13:52
  • 1
    @user984248 I updated my answer. addEntriesFromDictionary should work. – Jonathan Aug 27 '12 at 14:16
  • It works if you remove the loop and just use addEntriesFromDictionary. The accepted answer doesn't seem to work, but Pandey_Laxman's answer does. – Ian Clay May 12 '15 at 14:26
15
NSMutableDictionary * mutableDict = [NSMutableDictionary dictionary];

for (NSDictionary * formElements in sections)
{
    [mutableDict addEntriesFromDictionary:formElements];
}

This should work if It's correct that they don't share any keys.

Jonathan
  • 2,968
  • 3
  • 24
  • 36
1
NSMutableDictionary *mDict=[[NSMutableDictionary alloc]init];
    NSMutableDictionary *mDict2=[[NSMutableDictionary alloc]init];

//later suppose you have 5 object in mDict and 2 object in mDict2. combine in this Way.
    NSMutableArray *keys=[[NSMutableArray alloc]init];
    NSMutableArray *obj=[[NSMutableArray alloc]init];

    keys=[[mDict allKeys] mutableCopy];
    obj=[[mDict allValues] mutableCopy];

    [keys addObjectsFromArray:[mDict2 allKeys]];
    [obj addObjectsFromArray:[mDict2 allValues]];

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]initWithObjects:obj forKeys:keys];
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
0

You can enumerate your dictionaries with -objectEnumerator or other methodfs of NSDictionary.

So inside your loop you enumerate your dictionary and add all objects an keys into one big dictionary.

Engeor
  • 328
  • 4
  • 14
  • Can you please provide an example? Thats what I am looking for...one big dictionary at the end. – user984248 Aug 27 '12 at 13:43
  • Yes, I can, however, Pandey_Laxman solution is better. I completely forgot this method. If you still want example I can provide one but I think problem is in duplicitous keys as MrAzulay said. My solution will no help you with this. – Engeor Aug 27 '12 at 13:48
  • I understand. They have different keys tho. Let me see what I can do with an Array – user984248 Aug 27 '12 at 13:53