我正在尝试组合两个NSMutableDictionarys,但我遇到的问题是当我添加它时它只会覆盖另一个。例如,字典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;
}
);
}
我搜索了堆栈溢出和网络的其余部分,但找不到任何东西。我很抱歉,如果这已经得到解答,我找不到它,因为我没有找到正确的短语。
谢谢, 斯凯勒
答案 0 :(得分:1)
您可以遍历每个数据集并创建合并的数据集
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
替换合并数据,而不是将新combinedDictionary
创建为NSMutableDictionary
。
答案 1 :(得分:0)
字典基本上是一组键值对。你的字典A和字典2说明了这一点。但是在你的组合“词典”中,你所拥有的是一组两个词典,而不是一组键值对。换句话说,就是不可能组合这样的字典,因为你期望的结果不是字典。如果您想要一组词典,请使用NSSet
或NSArray
(推荐)
答案 2 :(得分:0)
这对你有帮助......
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);
如果你能让它变得简单,我就变得简单了 喜欢编码