我有这个 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
当我这样做时,它与数组一起工作得很好:
[someArray sortedArrayUsingSelector:@selector(compareToLocation:)];
但是...我想将它用作 NSFetchedResultsController 的 sortDescriptor,如下所示:
NSSortDescriptor *sortDistance = [[NSSortDescriptor alloc]
initWithKey:@"LocationObject"
ascending:YES
selector:@selector(compareToLocation:)];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDistance]];
实体中的“LocationObject”是“Transformable”,并存储为 CLLocation。
我在 PerformFetch 上得到了这个:
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不支持的 NSSortDescriptor 选择器:compareToLocation:”
预先感谢您的帮助 :)
我假设你正在使用 SQL 存储?
如果是这样,您可以使用的排序描述符会受到限制:
另一方面,SQL 存储将谓词和排序描述符编译为 SQL,并在数据库本身中评估结果。这样做主要是为了性能,但这意味着评估发生在非 Cocoa 环境中,因此依赖 Cocoa 的排序描述符(或谓词)无法工作。支持的排序选择器是compare: and 不区分大小写比较:, 本地化比较:, localizedCaseInsensitive比较:, and 本地化标准比较:(后者是类似 Finder 的排序方式,也是大多数人大多数时候应该使用的方式)。此外,您无法使用 SQLite 存储对瞬态属性进行排序。
From http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdPersistentStores.html#//apple_ref/doc/uid/TP40002875-SW11 http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdPersistentStores.html#//apple_ref/doc/uid/TP40002875-SW11
这意味着您无法在获取请求中执行您想要执行的操作。最简单的做法是在从数据库收到结果后对结果进行排序,假设您可以将其全部放入内存中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)