2012-06-28 95 views
0

我讀這個JSON文件:排序一個NSDictionary

{"longitude":["36.704765","46.704765", "47.704765"],"latitude":["-93.168274","-103.168274", "86.704765"]} 

這樣:

NSString *filenameMap = [NSString stringWithFormat:@"%@%@Map", destDir, NavBar.topItem.title]; 

NSString *MapPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@Map", NavBar.topItem.title]]; 

[self.restClient loadFile:filenameMap intoPath:MapPath]; 


NSString *fileContentMap = [[NSString alloc] initWithContentsOfFile:MapPath]; 

SBJsonParser *parserMap = [[SBJsonParser alloc] init]; 

NSDictionary *dataMap = (NSDictionary *) [parserMap objectWithString:fileContentMap error:nil]; 


NSArray *MaparrayLongitude = [dataMap objectForKey:@"longitude"]; 
NSArray *MaparrayLatitude = [dataMap objectForKey:@"latitude"]; 

//NSSortDescriptor *MaparrayLongitude = [[NSSortDescriptor alloc] initWithKey:@"longitude" ascending:NO selector:@selector(compare:)]; 
//NSSortDescriptor *MaparrayLatitude = [[NSSortDescriptor alloc] initWithKey:@"latitude" ascending:NO selector:@selector(compare:)]; 


NSDictionary* DictionaryMap = [NSDictionary dictionaryWithObjects:MaparrayLatitude forKeys:MaparrayLongitude]; 

每對在JSON文件(經度和緯度)座標繪製在MapView的註解,所以我有3個不同的地圖視圖,每個都有1個引腳。但地圖應該按順序排列:所以第一張地圖必須顯示第一對座標,第二張地圖,第二對座標...而當我按刷新時,地圖的順序會發生變化,如果我壓制,則會再次變化刷新...

所以我想要尊重JSON文件中的座標順序。

我試過,但沒辦法:

NSSortDescriptor *MaparrayLongitude = [[NSSortDescriptor alloc] initWithKey:@"longitude" ascending:NO selector:@selector(compare:)]; 
    NSSortDescriptor *MaparrayLatitude = [[NSSortDescriptor alloc] initWithKey:@"latitude" ascending:NO selector:@selector(compare:)]; 

請幫助!

編輯:

然後,我這樣做:

NSArray *allKeys = [Dictionary allKeys]; 
    for (int i = 0; i < [allKeys count]; i++) { 
     NSString *key = [allKeys objectAtIndex:i]; 
     NSObject *obj = [Dictionary objectForKey:key]; 

NSArray *allKeys2 = [DictionaryMap allKeys]; 






CGRect mapFrame = CGRectMake(400, e, 200, 110); 

MKMapView *mapView2 = [[MKMapView alloc] initWithFrame:mapFrame]; 
[image1 addSubview:mapView2]; 







    NSString *key2 = [allKeys2 objectAtIndex:i]; 
    NSObject *obj2 = [DictionaryMap objectForKey:key2]; 




    NSString *address = [NSString stringWithFormat:@"%@", obj2]; 
    float stringFloat = [address floatValue]; 
    float stringFloat2 = [key2 floatValue]; 

    CLLocationCoordinate2D anyLocation; 

    anyLocation.longitude = stringFloat; 

    anyLocation.latitude = stringFloat2; 







    MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init]; annotationPoint2.coordinate = anyLocation; 

    annotationPoint2.title = @"Event"; 
    annotationPoint2.subtitle = @"Microsoft's headquarters2"; 
    [mapView2 addAnnotation:annotationPoint2]; 


    [mapView2.userLocation setTitle:@"I am here"]; 

    [mapView2.userLocation addObserver:self 
           forKeyPath:@"location" 
            options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 

            context:NULL]; 

    [mapView2 setShowsUserLocation:NO]; 

    if (MapViewArray == nil)MapViewArray = [[NSMutableArray alloc]init]; 
    [MapViewArray addObject: mapView2]; 


    } 

所以,如果值是團結,我怎麼能annign一個經度和緯度值?

回答

2

字典沒有順序的概念,所以如果您需要按原始順序對齊經度/緯度對,則一個字典不是適合使用的數據結構。

你可以以各種方式解決這個問題,一個將是創建字典的陣列,其中每個字典表示一個對經度/緯度的:

//... 
NSArray *mapArrayLongitude = [dataMap objectForKey:@"longitude"]; 
NSArray *mapArrayLatitude = [dataMap objectForKey:@"latitude"]; 
if (mapArrayLongitude.count != mapArrayLatitude.count) { 
    //Error: Unequal number of longitude/latitude values 
} else { 
    NSMutableArray *pairs = [NSMutableArray array]; 
    for (int i = 0; i < mapArrayLongitude.count; i++) { 
     NSDictionary *pair = [NSDictionary dictionaryWithObjectsAndKeys: 
           [mapArrayLongitude objectAtIndex:i], @"longitude", 
           [mapArrayLatitude objectAtIndex:i], @"latitude", nil]; 
     [pairs addObject:pair]; 
    } 
    NSLog(@"First pair: %@", [pairs objectAtIndex:0]); 
    //... 
} 

您也可以簡單地保持兩個陣列稍後通過訪問兩個數組中的相同索引來重建對。

+0

看到編輯上面 – Alessandro

+0

看到編輯我已經發布,它繼續代碼 – Alessandro