1

My plist file:

   NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
   NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"];
   dict = [NSDictionary dictionaryWithContentsOfFile:path];

loop through Array, adding annotations and calculate distance:

 ann = [dict objectForKey:@"Blue"];
 [resultArray addObject:@"Blue"];


 for(int i = 0; i < [ann count]; i++) {


 NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

 double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
 double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

 MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
 CLLocationCoordinate2D theCoordinate;
 theCoordinate.latitude = realLatitude;
 theCoordinate.longitude = realLongitude;

 myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);
 myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
 myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
 myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];

 [mapView addAnnotation:myAnnotation];
 [annotations addObject:myAnnotation];

 CLLocation *pinLocation = [[CLLocation alloc]
                                           initWithLatitude:realLatitude
                                           longitude:realLongitude];

 CLLocation *userLocation = [[CLLocation alloc]
                                 initWithLatitude:mapView.userLocation.coordinate.latitude
                                            longitude:mapView.userLocation.coordinate.longitude];

 CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];

 NSLog(@"Distance: %4.0f m.", distance);

 }

My plist structure:

enter image description here

Now I need to add for all Dictionaries in "Blue" Array new Key named "Distance" with string distance

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Pavel Kaljunen
  • 1,291
  • 2
  • 26
  • 53

3 Answers3

1

You can Write these Lines inside your for() loop :

NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:@"Distance"];

GoodLuck !!!

Bhavin
  • 27,155
  • 11
  • 55
  • 94
0

A mutable dictionary can be changed, i.e. you can add and remove objects. create and add:

 NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
 [dict setObject:[NSNumber numberWithInt:42] forKey:@"A cool number"];
 int myNumber = [[dict objectForKey:@"A cool number"] intValue];
Arpit Kulsreshtha
  • 2,452
  • 2
  • 26
  • 52
0

As I have understood it will look like the following code:

    NSDictionary *rootDictionary = ...; //Your initialization
    NSString *blueKey = @"Blue";
    NSArray *blueArr = [rootDictionary objectForKey:blueKey];
    NSMutableArray *newBlueArr = [NSMutableArray array];
    for (NSDictionary *childDictionary in blueArr) {

        NSMutableDictionary *newChildDictionary = [NSMutableDictionary dictionaryWithDictionary:childDictionary];
        [newChildDictionary setValue:@"distance" forKey:@"Distance"];
        [newBlueArr addObject:newChildDictionary];
    }
    NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithDictionary:rootDictionary];
    [tempDictionary setValue:newBlueArr forKey:blueKey];
    rootDictionary = tempDictionary;

I guess someone could advice a simplier method because sometimes you don't need to convert an immutable object to a mutable one if you don't add or remove objects from array/dictionary.

user2159978
  • 2,629
  • 2
  • 16
  • 14