Pavel Kaljunen
Pavel Kaljunen

Reputation: 1291

How to add a key and string in dictionary?

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

Upvotes: 1

Views: 2239

Answers (3)

Bhavin
Bhavin

Reputation: 27235

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 !!!

Upvotes: 1

user2159978
user2159978

Reputation: 2709

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.

Upvotes: 0

Arpit Kulsreshtha
Arpit Kulsreshtha

Reputation: 2162

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

Upvotes: 0

Related Questions