我有一个 NSMutableArrayNSDictionary s 是这样的:

myArray (
    {
    chatins = 20;
    placeImageString = 244211112265925;
},
    {
    chatins = 5;
    placeImageString = 154909144575109;
},
    {
    chatins = 30;
    placeImageString = 193867280641162;
},
    {
    chatins = 13;
    placeImageString = 224627130902;
},

)
NSMutableArray 的另一个 NSDictionary 是这样的:
myArray2
(
    {
    category = "Local business";
    distance = "0.1";
    name = "Mts India";
    placeImageString = 244211112265925;
},
    {
    category = "Local business";
    distance = "0.17";
    name = "Aegis Ltd";
    placeImageString = 154909144575109;
},
    {
    category = "Automobiles and parts";
    distance = "0.19";
    name = Autopsyche;
    placeImageString = 78480207511;
},
    {
    category = Company;
    distance = "0.19";
    name = "The Oberoi, Gurgaon";
    placeImageString = 121676041233945;
},

)

我想合并 myArraymyArray2 以获得 NSMutableArray 的结果 NSDictionary 如下所示,其中我的 placeImageString 是匹配两个字典数组中数据的键,如果在 myArray2 中找不到键,那么 chatins 键的值应该是0.
myArray3
(
{
category = "Local business";
distance = "0.1";
name = "Mts India";
placeImageString = 244211112265925;
 chatins = 20;

},
{
category = "Local business";
distance = "0.17";
name = "Aegis Ltd";
placeImageString = 154909144575109;
chatins = 5;

},
{
category = "Automobiles and parts";
distance = "0.19";
name = Autopsyche;
placeImageString = 78480207511;
chatins = 0;


},
    {
    category = Company;
    distance = "0.19";
    name = "The Oberoi, Gurgaon";
    placeImageString = 121676041233945;
    chatins = 0;

    },
)

最佳答案

在这里,我为您提供了一个示例代码:

  NSMutableArray *array1 = [[NSMutableArray alloc] init];
  NSMutableArray *array2 = [[NSMutableArray alloc] init];


  NSMutableDictionary * dict = [[NSMutableDictionary alloc]
                                initWithObjects:[NSArray arrayWithObjects:@"1",@"ABC", nil]
                                forKeys:[NSArray arrayWithObjects:@"ID",@"NAME", nil]];
  [array1 addObject:dict];

  dict = nil;

  /*
   Same way added 2 more dictionaries to the same array - array1
   */

  NSLog(@"array1: %@", array1);


  dict = nil;

  dict = [[NSMutableDictionary alloc]
          initWithObjects:[NSArray arrayWithObjects:@"1",@"DEF", nil]
          forKeys:[NSArray arrayWithObjects:@"ID",@"ADDRESS", nil]];

  [array2 addObject:dict];

  dict = nil;

  /*
   Same way added 2 more dictionaries to the same array - array2
   */


  NSLog(@"array2: %@", array2);


  for (int index = 0; index < [array1 count]; index ++) {

    NSMutableDictionary *dict1 = [array1 objectAtIndex:index];

    for (NSMutableDictionary *dict2 in array2) {

      if ([[dict1 objectForKey:@"ID"] isEqualToString:
           [dict2 objectForKey:@"ID"]]) {

        [dict1 setObject:[dict2 objectForKey:@"ADDRESS"] forKey:@"ADDRESS"];
      }
    }
  }

现在最后,如果您检查 array1,则会在所有这些字典中添加另一个键 (ADDRESS)。

希望这有帮助。

-Mrunal

关于ios - 合并包含 NSDictionarys 的 NSArrays,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14482853/

10-14 23:49