I have this distanceSorter.h/distanceSorter.m:
@interface CLLocation (DistanceComparison)
- (NSComparisonResult) compareToLocation:(CLLocation *)other;
@end
@implementation CLLocation (DistanceComparison)
- (NSComparisonResult) compareToLocation:(CLLocation *)other {
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:[[NSUserDefaults standardUserDefaults] floatForKey:@"lastProcessedLatitude"] longitude:[[NSUserDefaults standardUserDefaults] floatForKey:@"lastProcessedLongitude"]];
CLLocationDistance thisDistance = [self distanceFromLocation:currentLocation];
CLLocationDistance thatDistance = [other distanceFromLocation:currentLocation];
if (thisDistance < thatDistance) { return NSOrderedAscending; }
if (thisDistance > thatDistance) { return NSOrderedDescending; }
return NSOrderedAscending;
}
@end
It works fine with Arrays when i do this:
[someArray sortedArrayUsingSelector:@selector(compareToLocation:)];
but...I want to use it as a sortDescriptor of a NSFetchedResultsController like so:
NSSortDescriptor *sortDistance = [[NSSortDescriptor alloc]
initWithKey:@"LocationObject"
ascending:YES
selector:@selector(compareToLocation:)];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDistance]];
The "LocationObject" in the entity is a "Transformable" and is stored as a CLLocation.
I am getting this on the performFetch:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: compareToLocation:'
THANK YOU IN ADVANCE FOR YOUR HELP :)