Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add to NSDictionary entries to NSMutableDictionary

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"];        
}
like image 626
user984248 Avatar asked Aug 27 '12 13:08

user984248


People also ask

How do you convert NSDictionary to NSMutableDictionary?

Use -mutableCopy . NSDictionary *d; NSMutableDictionary *m = [d mutableCopy]; Note that -mutableCopy returns id ( Any in Swift) so you will want to assign / cast to the right type. It creates a shallow copy of the original dictionary.

How do you add value in NSMutableDictionary?

[myDictionary setObject:nextValue forKey:myWord]; You can simply say: myDictionary[myWord] = nextValue; Similarly, to get a value, you can use myDictionary[key] to get the value (or nil).

What is the difference between NSDictionary and NSMutableDictionary?

Main Difference is:NSMutableDictionary is derived from NSDictionary, it has all the methods of NSDictionary. NSMutableDictionary is mutable( can be modified) but NSDictionary is immutable (can not be modified).

Is NSDictionary ordered?

NSDictionary keys & values are not ordered.


2 Answers

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.

like image 149
Jonathan Avatar answered Nov 14 '22 11:11

Jonathan


you can add dictionary object like below.

NSMutableDictionary *mDict=[NSMutableDictionary dictionary];
    [mDict addEntriesFromDictionary:DictObj];
like image 26
Pandey_Laxman Avatar answered Nov 14 '22 12:11

Pandey_Laxman