Question

I have made a NSDictionary HospitalDictionary which holds latitude,longitude and distances

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];
{ 

Then data is short using following code

// 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]);

Output of this is -

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

I want to get only Latitude or Longitude for the ObjectAtIndex:1 for the sortedArray. How can i get the particular value ie. latitude out of sortedArray not the complete list.

Was it helpful?

Solution

To access a property/method of an object in an array, you can use something like this:

NSLog(@"Latitude for object at index 1 is %@",[[sortedArray objectAtIndex:1] latitude]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top