2013-05-04 82 views
0

我的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

現在我需要添加所有Dictionaries「藍色」ArrayKey名爲「距離「與string距離

+0

http://stackoverflow.com/questions/3159844/how-to-add-to-an-nsdictionary這可以幫助你 – 2013-05-04 11:16:29

回答

0

你可以寫你的裏面這幾行for()循環:

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

好運!

+0

但爲什麼它爲所有字典增加相同的價值? NSLog(@「Distance:%4.0f m。」,distance);值不同 – 2013-05-04 11:55:56

+0

NSString * dist = [NSString stringWithFormat:@「%4.0f m。」,distance]; – 2013-05-04 11:56:47

+0

[temp setValue:dist forKey:@「Distance」]; – 2013-05-04 11:57:37

0

可變字典可以更改,即您可以添加和刪除對象。 創建並添加:

NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10]; 
[dict setObject:[NSNumber numberWithInt:42] forKey:@"A cool number"]; 
int myNumber = [[dict objectForKey:@"A cool number"] intValue]; 
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; 

我想有人能指點一個simplier方法,因爲有時候你不需要轉換的不變如果不添加或刪除數組/字典中的對象,則將對象設置爲可變對象。