- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有这个 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 上得到这个:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: compareToLocation:'
提前感谢您的帮助:)
最佳答案
我假设您正在使用 SQL 存储?
如果是这样,您可以使用的排序描述符有限:
The SQL store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself. This is done primarily for performance, but it means that evaluation happens in a non-Cocoa environment, and so sort descriptors (or predicates) that rely on Cocoa cannot work. The supported sort selectors are compare: and caseInsensitiveCompare:, localizedCompare:, localizedCaseInsensitiveCompare:, and localizedStandardCompare: (the latter is Finder-like sorting, and what most people should use most of the time). In addition you cannot sort on transient properties using the SQLite store.
这意味着您无法执行您在获取请求中尝试执行的操作。最简单的做法是在从数据库中收到结果后对其进行排序,假设您可以将其全部放入内存。
关于iphone - 使用 NSComparisonResult 对 CoreData 实体进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7293996/
在我的 iOS 应用程序中,我实现了一个来自核心数据的表格 View 和一个搜索栏。在当前状态下,搜索只显示以搜索字符串开头的字段,但我想要的是显示包含至少三个字符长度的搜索文本的字段。 这是我的搜索
我的应用程序正在比较发送到函数的请求字符串,例如 -(void)sendSearchRequest:(NSString *)request sport:(NSString *)sport locati
我将 NSComparisonResult 与我的 SearchController 一起使用: for (Annotation *ano in listContent) { NSCo
我使用 NSComparisonResult 搜索数组,但是当我使用 NSDiacriticInsensitiveSearch 时,它无法立即找到 å æ ø 等斯堪的纳维亚字母,但它可以找到 á 等
当我在搜索字段中输入两个以上的字符时会引发异常。我使用 NSComparisonResult 在 UITableView 上显示结果: - (void)filterContentForSearchTe
我需要在 Swift 中比较两次并使用 NSComparisonResult 我可以获得正确的结果,直到晚上 10 点到晚上 11:59 之间的时间。这些时间显示相反的结果。任何人都知道这是什么问题?
我有一个基于 Swift 的 iOS 应用程序。我正在尝试使用 Swift 排序方法之一按“createdAt”NSDate 属性对我的对象数组进行排序。 我的数组有一堆 PFObject - 这些对
什么是 NSComparisonResult 和 NSComparator? 我见过其中一种类型定义,类似这样: typedef NSComparisonResult (^NSComparator)(
我正在使用此代码在我的表格 View 中进行搜索: for (Actor *actorz in arr) { NSComparisonResult result = [actorz.
我正在使用此代码在我的表格 View 中进行搜索: for (Actor *actorz in arr) { NSComparisonResult result = [actorz.
我的目的是让从 "1927-2-2" 到 "1928-1-22" 的每个日期在输入 UIDatePicker 时都调用图像。我可以用除“1928-1-22”之外的每个日期成功调用图像,这是唯一不产生图
我正在尝试使用 NSComparisonResult 按特定顺序对我的数组进行排序。我无法弄清楚如何实现我想要的序列。 我正在尝试将表情符号置于顶部(表情符号的顺序无关紧要),然后是 A-Z 字母,在
我有一段代码对点的 NSMutableArray 进行排序,如下所示: [points sortUsingComparator:^NSComparisonResult (id firstObject,
NSDecimalNumber *no1 = [NSDecimalNumber decimalNumberWithString:[totalPotFund stringByReplacingOc
我有这个 distanceSorter.h/distanceSorter.m: @interface CLLocation (DistanceComparison) - (NSComparisonRe
我是一名优秀的程序员,十分优秀!