在NSMutableArray中合并两个可变字典

时间:2013-02-14 11:02:21

标签: objective-c nsmutabledictionary

我有一个包含服务器数据的数组。我无法改变服务器的响应。

({
        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;
    }
    )

我不知道怎么做任何帮助都非常感谢。 谢谢你

4 个答案:

答案 0 :(得分:1)

NSMutableArray *myArray;
[myArray[1] addEntriesFromDictionary:myArray[2]];
[myArray removeObjectAtIndex:2];

答案 1 :(得分:0)

NSMutableDictionary *dicMerged  = [NSMutableDictionary dictionaryWithDictionary:[YourArray objectAtIndex:1]];
[dicMerged addEntriesFromDictionary:[YourArray objectAtIndex:2]];
 NSMutableArray *artFinal = [NSMutableArray array];
 [artFinal addObject:[YourArray objectAtIndex:0]];
 [artFinal addObject:dicMerged];

print artFinal you will get as you want

答案 2 :(得分:0)

如果我理解你,请尝试:

[[yourMutableArray objectAtIndex:1] addEntriesFromDictionary:[yourMutableArray objectAtIndex:2]];  
[yourMutableArray removeLastObject];

答案 3 :(得分:0)

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;
    }
    )