Search code examples
iosobjective-cjsonnsarraymkmapview

Compare/Lookup two NSArray and get the correct value


In my project I have two different datasets stored in two seperated arrays. Now I'm trying to do a lookup/comparing if some fields are equal and then add the extra values, for example:

Dataset 1:

for ( int i=0; i<[self.plarray count]; i++)
    {
        Places *object2 = [self.plarray objectAtIndex:i];
        ePlaces = object2.pCitpr;

        NSLog(@"FD: %@", ePlaces);
    }

Dataset 2:

for ( int i=0; i<[self.data count]; i++)
    {

         Mpobject *object3 = [self.data objectAtIndex:i];
         ePlaces2 = object3.oLocation;

        NSLog(@"MP: %@", ePlaces2);

    }

The second dataset returns a lower count, but the output is also stored in dataset 1. Dataset 1 holds extra values such as (lon/lat) GPS coordinations which I wanna add to Dataset 2.

So, check if dataset1 value is equal to dataset2 value, then add the extra (lon/lat) values to dataset1.

The reason why I need do this is because I have a JSON file in my project which holds GPS coordinations from all the cities and need to set it as a annotation in my mapView.

Now... Dataset 2 only has the cityname so without the coordinates, and I could do a reverse lookup for the coordinates using CLGeocoder but it will give warnings if there are too many requests. Apple's documentation also says you can only do one lookup every minute.

I hope it's understandable.

Ps. excuse my language, I'm from the Netherlands.


Solution

  • It's hard to know how to provide code for this without knowing the properties of each object, but you just need to iterate over both datasets and compare the relevant property, then assign the values.

       for (int i =0; i<[self.plarray count]; i++) {
            for (int j = 0; j<[self.data count]; j++) {
    
                Places *object = [self.plarray objectAtIndex:i];
                Mpobject *object1 = [self.data objectAtIndex:j];
    
                if ([object.name isEqualToString: object1.name])
                {
                    object1.lat = object.lat;
                    object1.lon = object.lon;
                }
            }
        }