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

Posted

技术标签:

【中文标题】如何在字典中添加键和字符串?【英文标题】:How to add a key and string in dictionary? 【发布时间】:2013-05-04 10:47:49 【问题描述】:

我的 plist 文件:

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

遍历Array,添加注解并计算距离:

 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 结构:

现在我需要为“蓝色”中的所有Dictionaries 添加Array 新的Key 命名为“距离”和string 距离

【问题讨论】:

***.com/questions/3159844/… 这对你有帮助 【参考方案1】:

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

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

祝你好运!!!

【讨论】:

但为什么它为所有字典添加相同的值? NSLog(@"距离: %4.0f m.", 距离);价值观不同 NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance]; [temp setValue:dist forKey:@"Distance"]; @PavelKaljunen:我给了你中心思想,你可以根据你的要求修改它。【参考方案2】:

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

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

【讨论】:

【参考方案3】:

据我了解,它看起来像以下代码:

    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;

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

【讨论】:

以上是关于如何在字典中添加键和字符串?的主要内容,如果未能解决你的问题,请参考以下文章

两个字典(键和值)的递归差异?

在字典中添加 NSIndexPath 作为键和 NSNumber 作为值的问题?

第 6 章 字典

保存Python字典时,json为每个条目添加一个字符[重复]

如何从字典中检查特定字符串的键[重复]

Javascript:在嵌套字典中添加键/值对;键和值是变量