Objective c 使用相同的键组合两个NSMutableDictionary

Objective c 使用相同的键组合两个NSMutableDictionary,objective-c,nsmutabledictionary,Objective C,Nsmutabledictionary,我试图合并两个NSMutableDictionary,但遇到的问题是,当我添加它们时,它只会覆盖另一个。例如,字典A具有以下数据: { StationLogs = ( { log = "This is the Log Text"; timeStamp = "2014-01-04 04:31:04 +0000"; title = Title; } ); } 字典

我试图合并两个NSMutableDictionary,但遇到的问题是,当我添加它们时,它只会覆盖另一个。例如,字典A具有以下数据:

{
    StationLogs = (
                {
            log = "This is the Log Text";
            timeStamp = "2014-01-04 04:31:04 +0000";
            title = Title;
        }
    );
}
字典2会有这样的数据:

{
        StationLogs = (
        {
            log = "logText";
            timeStamp = "2014-01-04 04:35:04 +0000";
            title = Title2;
        }
    );
}
我如何让它们合并成这样:

    {
        StationLogs = (
        {
            log = "This is the Log Text";
            timeStamp = "2014-01-04 04:31:04 +0000";
            title = Title;
        },
        {
            log = "logText";
            timeStamp = "2014-01-04 04:35:04 +0000";
            title = Title2;
        }
    );
}
我已经在stack overflow和web的其他部分进行了搜索,但没有找到任何内容。我很抱歉,如果这已经得到了回答,我无法找到它,因为我没有搜索正确的短语

谢谢,
Skylar

字典基本上是一组键值对。你的字典A和字典2说明了这一点。但在组合的“字典”中,您拥有的是一组两个字典,而不是一组键值对。换句话说,像这样组合字典是不可能的,因为您期望的结果不是字典。如果需要一组词典,请使用
NSSet
NSArray
(推荐)

您可以迭代每个数据集并创建合并的数据集

    NSMutableDictionary *dict1; //Your first set of data
    NSMutableDictionary *dict2; //Your second set of data

    NSArray *keys1 = [dict1 allKeys];
    NSArray *keys2 = [dict2 allKeys];

    NSMutableDictionary *combinedDictionary = [NSMutableDictionary dictionary];

    //Iterating through first data set
    for (id key in keys1) {
        NSArray *array = [dict1 objectForKey:key];

        NSMutableArray *subArray = [combinedDictionary objectForKey:key];
        if (!subArray) {
            subArray = [NSMutableArray array];
            [combinedDictionary setObject:subArray forKey:key];
        }

        [subArray addObjectsFromArray:array];
    }

    //Iterating through second data set
    for (id key in keys2) {
        NSArray *array = [dict2 objectForKey:key];

        NSMutableArray *subArray = [combinedDictionary objectForKey:key];
        if (!subArray) {
            subArray = [NSMutableArray array];
            [combinedDictionary setObject:subArray forKey:key];
        }

        [subArray addObjectsFromArray:array];
    }
您可以在现有的
NSMutableDictionary
中替换其中一个
NSMutableDictionary
中的合并数据,而不是创建新的
NSMutableDictionary

这是否有助于您

NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"This is the Log Text",@"log",@"2014-01-04 04:31:04 +0000",@"timeStamp",@"Title",@"title", nil];

NSMutableDictionary *dict1=[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Log Text",@"log",@"2014-01-04 04:35:04 +0000",@"timeStamp",@"Title2",@"title", nil];

NSArray *arr=[NSArray arrayWithObject:dict];
NSArray *arr1=[NSArray arrayWithObject:dict1];

//here is your first dictnary
NSMutableDictionary *dictm1=[[NSMutableDictionary alloc]initWithObjectsAndKeys:arr,@"StationLogs", nil];


NSLog(@"%@",dictm1);

 //here is your second dictionary
NSMutableDictionary *dictm2=[[NSMutableDictionary alloc]initWithObjectsAndKeys:arr1,@"StationLogs", nil];
NSLog(@"%@",dictm2);

//now here you get array of dictnary which is storted with key value "StationLogs" for first didtnary
NSArray *tempArray=[[NSArray alloc]initWithArray:[dictm1 objectForKey:@"StationLogs"]];
NSLog(@"%@",tempArray);

  //now here you get array of dictnary whaich is storted with key value "StationLogs" for second didtnary
NSMutableArray *tempArray1=[[NSMutableArray alloc]initWithArray:[dictm2 objectForKey:@"StationLogs"]] ;

[tempArray1 addObjectsFromArray:tempArray];

//now adding value of second dictionary to first dict
[dictm1 setValue:tempArray1 forKey:@"StationLogs"];

NSLog(@"%@",dictm1);
我说的很简单,如果你能做到的话
就像编码一样

只需为每个值使用不同的键即可。使用字典是最基本的-你应该读一本编程书,甚至只是苹果的objective-c和类参考文档,因为如果你尝试编写没有任何基础的代码,那么你会遇到很多问题。你不能用相同的密钥来添加已经在DICT中可用的数据。如果你想得到它,就要为关键的日志记录一个命令和添加数组,现在你把AdRund添加到ARRY中,这是因为你将得到相同的数据。同样的关键是,我需要能够将数据添加到同一个集合中,以获得多条消息,该程序连接大约35个工作站,每个工作站都有自己的数据集,当他们提交数据时,我想将其添加到存档中,最好的方法是将它们分开,而不是将它们全部扔进去。我可能错了,但在我的情况下,这就是程序的其余部分是如何编写的。嘿,如果你想这样做的话,它不需要用不同的键来存储。这不是一个大问题,也不是很小的工作,但你必须围绕着一点来获得它,或者使用数组来实现上面的请求。好的,等等,我会给你发送一些代码,让你知道如何实现它it@OstlerDev那是你的吗需要什么?