简体   繁体   English

将NSDictionary条目添加到NSMutableDictionary

[英]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.

相关问题 iOS - 区分NSDictionary和NSMutableDictionary? - iOS — distinguish an NSDictionary from an NSMutableDictionary? 是否可以将NSdictionary对象转换为NSMutableDictionary? - Is it ok to cast a NSdictionary object to NSMutableDictionary? 如何将 NSDictionary 转换为 NSMutableDictionary? - How can I convert an NSDictionary to an NSMutableDictionary? NSMutableDictionary SetObject:ForKey:未插入NSDictionary对象 - NSMutableDictionary SetObject:ForKey: is not inserting NSDictionary object 从NSDictionary到NSMutableDictionary的数据传递问题 - Problems with passing of data from NSDictionary to NSMutableDictionary NSDictionary 可能无法响应 NSMutableDictionary object 上的 setObject:forKey - NSDictionary may not respond to setObject:forKey on a NSMutableDictionary object 循环浏览 NSDictionary 中的条目 - cycle through entries in NSDictionary 我们如何存储到NSDictionary中? NSDictionary和NSMutableDictionary有什么区别? - How can we store into an NSDictionary? What is the difference between NSDictionary and NSMutableDictionary? 使用类型为“ NSDictionary *”的表达式初始化“ NSMutableDictionary *”的指针类型不兼容 - Incompatible pointer types initializing 'NSMutableDictionary *' with an expression of type 'NSDictionary *' 我如何获得NSDictionary / NSMutableDictionary的原始订单? - How can i get Original order of NSDictionary/NSMutableDictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM