Combining NSArray with NSDictionarys

I have NSMutableArray

of NSDictionary

something like this:

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

      

)

and more NSMutableArray

from NSDictionary

something like this:

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

      

)

I want to combine myArray

and myArray2

to get NSMutableArray

of NSDictionary

something like below, where my placeImageString

is the key that matches the data in both arrays of dictionaries, and if the key is not found in myArray2

then the key value chatins

must be 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;

    },
)

      

+3


source to share


2 answers


I have a sample code here:

  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"];
      }
    }
  }

      

Now at the end, if you check with array 1, another key (ADDRESS) is added in this whole dictionary.



Hope for this help.

-Mrunal

+2


source


1. Create a new empty NSMutableArray newArray
2. Loop through all NSDictionaries of myArray2

    a. Copy the current NSDictionary to a new NSMutableDictionary newDict
    b. Loop through myArray1
        b1. if myArray1.dict.placeImageString == newDict.placeImageString
            - Add myArray1.dict.chatins to newDict
            - break out of loop
    c. add newDict to newArray

      



0


source







All Articles