[英]Add to NSDictionary entries to NSMutableDictionary
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. 我试过
addEntriesFromDictionary
,但它无法正常工作。 Thanks for your help. 谢谢你的帮助。
for (int i=0; i<sections.count; i++){
formElements = [[sections objectAtIndex:i]objectForKey:@"Dictionary"];
}
you can add dictionary object like below. 你可以添加如下的字典对象。
NSMutableDictionary *mDict=[NSMutableDictionary dictionary];
[mDict addEntriesFromDictionary:DictObj];
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. 如果它们不共享任何键是正确的,这应该有效。
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];
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. 因此,在循环中,您枚举字典并将所有对象的键添加到一个大字典中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.