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"];
}
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.
[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).
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).
NSDictionary keys & values are not ordered.
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.
you can add dictionary object like below.
NSMutableDictionary *mDict=[NSMutableDictionary dictionary];
[mDict addEntriesFromDictionary:DictObj];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With