Objective c 在NSMutableArray中合并两个可变字典
我有一个包含来自服务器的数据的数组。我无法更改服务器的响应Objective c 在NSMutableArray中合并两个可变字典,objective-c,nsmutabledictionary,Objective C,Nsmutabledictionary,我有一个包含来自服务器的数据的数组。我无法更改服务器的响应 ({ ObservationMapId = 13; "Observation_Type" = Results; actionItem = "Tesing action"; followupdate = "February 14, 2013"; followupstatus = Open; observation = "Testing resu
({
ObservationMapId = 13;
"Observation_Type" = Results;
actionItem = "Tesing action";
followupdate = "February 14, 2013";
followupstatus = Open;
observation = "Testing results for image";
},
{
ObservationMapId = 13;
"Observation_Type" = Results;
observation = "Demo observation";
},
{ actionItem = "Tesing action";
followupdate = "February 14, 2013";
followupstatus = Open;
}
)
我想把第二本词典和第三本词典合并如下
({
ObservationMapId = 13;
"Observation_Type" = Results;
actionItem = "Tesing action";
followupdate = "February 14, 2013";
followupstatus = Open;
observation = "Testing results for image";
},
{
ObservationMapId = 13;
"Observation_Type" = Results;
observation = "Demo observation";
actionItem = "Tesing action";
followupdate = "February 14, 2013";
followupstatus = Open;
}
)
我不知道如何进行,非常感谢您的帮助。
adv中的谢谢如果我理解正确,请尝试:
NSMutableArray *myArray;
[myArray[1] addEntriesFromDictionary:myArray[2]];
[myArray removeObjectAtIndex:2];
[[yourMutableArray objectAtIndex:1] addEntriesFromDictionary:[yourMutableArray objectAtIndex:2]];
[yourMutableArray removeLastObject];
[[yourMutableArray objectAtIndex:1] addEntriesFromDictionary:[yourMutableArray objectAtIndex:2]];
[yourMutableArray removeLastObject];
take the server response in an array-
NSArray *array_Resp; and put server response ({
ObservationMapId = 13;
"Observation_Type" = Results;
actionItem = "Tesing action";
followupdate = "February 14, 2013";
followupstatus = Open;
observation = "Testing results for image";
},
{
ObservationMapId = 13;
"Observation_Type" = Results;
observation = "Demo observation";
},
{ actionItem = "Tesing action";
followupdate = "February 14, 2013";
followupstatus = Open;
}
)
into this array.
// now take a dictionary
NSMutableDictionary *dictMerged = [NSMutableDictionary new];
dictMerged = [[array_Resp objectAtIndex:1] mutableCopy];
NSDictionary *thirdDict = [array_Resp objectAtIndex:2];
for (id key in [thirdDict allKeys]) {
[dictMerged setObject:[thirdDict objectForKey:key] forKey:key];
}
// Now your dictMerged has merged values from 2nd and third dict from array_Resp
NSMutableArray *desiredArray = [NSMutableArray arrayWithObjects:dictMerged, nil];
this desired array will have your required merged dictionaries as
({
ObservationMapId = 13;
"Observation_Type" = Results;
actionItem = "Tesing action";
followupdate = "February 14, 2013";
followupstatus = Open;
observation = "Testing results for image";
},
{
ObservationMapId = 13;
"Observation_Type" = Results;
observation = "Demo observation";
actionItem = "Tesing action";
followupdate = "February 14, 2013";
followupstatus = Open;
}
)