如何在字典中添加键和字符串?

时间:2013-05-04 10:47:50

标签: ios objective-c nsarray plist nsdictionary

我的plist文件:

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

循环数组,添加注释并计算距离:

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

 }

我的plist结构:

enter image description here

现在我需要添加名为“距离”的“蓝色”DictionariesArray中的所有Key string 距离

3 个答案:

答案 0 :(得分:0)

您可以在for()循环中写下这些行:

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

GoodLuck !!!

答案 1 :(得分:0)

可以更改可变字典,即可以添加和删除对象。 创建并添加:

 NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
 [dict setObject:[NSNumber numberWithInt:42] forKey:@"A cool number"];
 int myNumber = [[dict objectForKey:@"A cool number"] intValue];

答案 2 :(得分:0)

据我所知,它看起来像下面的代码:

    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;

我想有人可以建议一个更简单的方法,因为如果你不从数组/字典中添加或删除对象,有时你不需要将不可变对象转换为可变对象。