I have a for loop that goes through a series of dictionaries in an array.
我有一个for循环,通过一个数组中的一系列字典。
How can I consolidate all the dictionaries entries as it goes through the for loop into one NSMutableDictionary
?
如何通过for循环将所有词典条目整合到一个NSMutableDictionary中?
I tried addEntriesFromDictionary
, but its not working. Thanks for your help.
我试过addEntriesFromDictionary,但它无法正常工作。谢谢你的帮助。
for (int i=0; i<sections.count; i++){
formElements = [[sections objectAtIndex:i]objectForKey:@"Dictionary"];
}
4 个解决方案
#1
14
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.
如果它们不共享任何键是正确的,这应该有效。
#2
14
you can add dictionary object like below.
你可以添加如下的字典对象。
NSMutableDictionary *mDict=[NSMutableDictionary dictionary];
[mDict addEntriesFromDictionary:DictObj];
#3
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];
#4
0
You can enumerate your dictionaries with -objectEnumerator or other methodfs of NSDictionary.
您可以使用-objectEnumerator或NSDictionary的其他方法f枚举您的词典。
So inside your loop you enumerate your dictionary and add all objects an keys into one big dictionary.
因此,在循环中,您枚举字典并将所有对象添加到一个大字典中。
#1
14
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.
如果它们不共享任何键是正确的,这应该有效。
#2
14
you can add dictionary object like below.
你可以添加如下的字典对象。
NSMutableDictionary *mDict=[NSMutableDictionary dictionary];
[mDict addEntriesFromDictionary:DictObj];
#3
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];
#4
0
You can enumerate your dictionaries with -objectEnumerator or other methodfs of NSDictionary.
您可以使用-objectEnumerator或NSDictionary的其他方法f枚举您的词典。
So inside your loop you enumerate your dictionary and add all objects an keys into one big dictionary.
因此,在循环中,您枚举字典并将所有对象添加到一个大字典中。