我制作了一个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";
}
我想只获得ObjectAtIndex的纬度或经度:1为sortedArray。我怎样才能得到特定的价值,即。 latitude of sortedArray不是完整列表。
答案 0 :(得分:3)
要访问数组中对象的属性/方法,可以使用以下内容:
NSLog(@"Latitude for object at index 1 is %@",[[sortedArray objectAtIndex:1] latitude]);