Enumerate Latitude and Longitude from Plist
I want to sort a tableView of places by distance closest to the user. I have three methods (myLocation, clubLocation and enumerate). The lat's and long's are held on a plist (objectatIndex 4 and 5 respectively for the code below). I've created the two arrays, one for lat's and one for long's below in the enumeration method, but I'm having a mental block on how to connect everything together. I would assume the enumerate would return an array (instead of void, that's just a place holder). How can it return two arrays at once? Do I enumerate once for lat and once for long? Please help.
I've updated to have two meth开发者_StackOverflowods one for Lat and one for Long, how do I return the distances and their associated objects? For example, an array of just distances does me no good, as I need to know which distance is associated to which location?
-(NSArray *)distanceFromLat
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"];
NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *latitudeArray = [[NSMutableArray alloc] init];
latitudeArray = [[tempDictionary objectForKey:@"Subtree"]objectAtIndex:4];
NSEnumerator *enumLat = [latitudeArray objectEnumerator];
id object;
while ((object = [enumLat nextObject])) {
[myLocation distanceFromLocation:clubLocation];
}
}
-(NSArray *)distanceFromLong
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"];
NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *longitudeArray = [[NSMutableArray alloc] init];
longitudeArray = [[tempDictionary objectForKey:@"Subtree"]objectAtIndex:5];
NSEnumerator *enumLong = [longitudeArray objectEnumerator];
id object;
while ((object = [enumLong nextObject])) {
[myLocation distanceFromLocation:clubLocation];
}
}
-(CLLocation *)clubLocation
{
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"]];
NSArray *array = [dictionary objectForKey:@"Subtree"];
NSNumber *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]];
NSNumber *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]];
double clubLatitude = [latitude doubleValue];
double clubLongitude = [longitude doubleValue];
clubLocation = [[CLLocation alloc]initWithLatitude:clubLatitude longitude:clubLongitude];
return clubLocation;
}
-(CLLocation *)myLocation
{
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude];
NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude];
double myLatitudeD = [myLatitude doubleValue];
double myLongitudeD = [myLongitude doubleValue];
myLocation = [[CLLocation alloc]initWithLatitude:myLatitudeD longitude:myLongitudeD];
return myLocation;
}
Returning more than 1 value can be done in at least three simple ways
using a custom struct (struct Pair { id first; id second; })
using an nsarray to hold both objects
using pointers-to-variables as arguments (enumerationWithFirst: (NSArray*) first second: (NSArray*) second
Using Objective-C++ adds a few more options, but these should do.
By the way, using two "enumerator" methods makes more sense. You're essentially returning properties from a simple data structure.
Oh, and please check your code for leaks. There are a few, if I'm not mistaken.
精彩评论