1

我制作了一个包含纬度、经度和距离的 NSDictionary HospitalDictionary

NSMutableArray *HospitalArray = [NSMutableArray array];
NSDictionary *HospitalDictionary = [[NSDictionary alloc]init];

NSString *LATITUDE = @"latitude";
NSString *LONGITUDE = @"longitude";
NSString *DISTANCE = @"distance";

for (int i=0; i<=100; i++) { 
// calculations for coordinates and distance are done ....
HospitalDictionary = [NSDictionary dictionaryWithObjectsAndKeys:latitude, LATITUDE,
        longitude, LONGITUDE,[NSNumber numberWithInt:distanceInKm], DISTANCE, nil];

[HospitalArray addObject:HospitalDictionary];
{ 

然后使用以下代码数据很短

// These line are added to short the Dictionary added to Array List with the Key of Distance 
    NSSortDescriptor *distanceDescriptor = [[NSSortDescriptor alloc] initWithKey:DISTANCE ascending:YES];
    id obj;
    NSEnumerator * enumerator = [HospitalArray objectEnumerator];
    NSArray *descriptors = [NSArray arrayWithObjects:distanceDescriptor, nil];
    NSArray *sortedArray = [HospitalArray sortedArrayUsingDescriptors:descriptors];

    enumerator = [sortedArray objectEnumerator];
    // this will print out the shorted list for DISTANCE
    while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);

    // this will return the object at index 1 
    NSLog(@"Selected array is %@",[sortedArray objectAtIndex:1]);

这个的输出是 -

2012-05-30 08:24:42.784 Hospitals[422:15803] 
sorted array is 
{
    distance = 1;
    latitude = "27.736221";
    longitude = "85.330095";
}

我只想获取 sortedArray 的 ObjectAtIndex:1 的纬度或经度。我怎样才能获得特定的价值,即。sortedArray 中的纬度不是完整列表。

4

1 回答 1

3

要访问数组中对象的属性/方法,可以使用如下内容:

NSLog(@"Latitude for object at index 1 is %@",[[sortedArray objectAtIndex:1] latitude]);
于 2012-05-30T03:27:10.373 回答