gpt4 book ai didi

ios - NSSortDescriptor 和 NSFetchedResultsController 的自定义选择器

转载 作者:可可西里 更新时间:2023-11-01 05:04:22 25 4
gpt4 key购买 nike

我遇到了一些麻烦,我想知道是否有人可以提供一些指导。

我正在尝试按以下顺序创建节标题:["Push"、"Busy"、"Finished"、"Canceled"]。

不幸的是,这个顺序不能用简单的“升序是/否”来实现......我必须添加一个自定义比较选择器......像这样......:

**“states”映射到 [“Push”、“Busy”、“Finished”、“Canceled”]

NSSortDescriptor *sortStates = [NSSortDescriptor sortDescriptorWithKey:@"states"
ascending:NO
selector:@selector(compareCustom:)];

因此,我实现了一个“compareCustom”...然而,结果是我无法实现一个自定义排序器,因为出现了以下异常结果:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不支持的 NSSortDescriptor 选择器:compareCustom:”

作为引用,sortDate 的实现方式是:

request.sortDescriptors = @[sortStates]
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:@"states"
cacheName:nil];

看来我无法实现返回 NSComparisonResult 的 compareCustom?我觉得这应该是可能的..但我无法弄清楚如何。如果有人可以提供一些建议,我将不胜感激!

也供引用,这是我的“compareCustom”方法

-(NSComparisonResult)compareCustom:(NSString *)anotherState {
if ([(NSString *)self isEqualToString:@"Push"] && [anotherState isEqualToString:@"Canceled"]) {
return NSOrderedDescending;
}

else if ([(NSString *)self isEqualToString:@"Push"] && [anotherState isEqualToString:@"Busy"]) {
return NSOrderedDescending;
}

else if ([(NSString *)self isEqualToString:@"Push"] && [anotherState isEqualToString:@"Finished"]) {
return NSOrderedDescending;
}

else if ([(NSString *)self isEqualToString:@"Push"] && [anotherState isEqualToString:@"Push"]) {
return NSOrderedSame;
}

else if ([(NSString *)self isEqualToString:@"Finished"] && [anotherState isEqualToString:@"Canceled"]) {
return NSOrderedDescending;
}

else if ([(NSString *)self isEqualToString:@"Finished"] && [anotherState isEqualToString:@"Busy"]) {
return NSOrderedAscending;
}
else if ([(NSString *)self isEqualToString:@"Finished"] && [anotherState isEqualToString:@"Finished"]) {
return NSOrderedSame;
}
else if ([(NSString *)self isEqualToString:@"Finished"] && [anotherState isEqualToString:@"Push"]) {
return NSOrderedAscending;
}

else if ([(NSString *)self isEqualToString:@"Busy"] && [anotherState isEqualToString:@"Finished"]) {
return NSOrderedDescending;
}
else if ([(NSString *)self isEqualToString:@"Busy"] && [anotherState isEqualToString:@"Canceled"]) {
return NSOrderedDescending;
}

else if ([(NSString *)self isEqualToString:@"Busy"] && [anotherState isEqualToString:@"Busy"]) {
return NSOrderedSame;
}

else if ([(NSString *)self isEqualToString:@"Busy"] && [anotherState isEqualToString:@"Push"]) {
return NSOrderedAscending;
}

else {
return NSOrderedSame;
}
}

最佳答案

根据documentation自定义排序不适用于 SQLite 持久存储:

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.

所以看起来您不能简单地强制 NSFetchedResultsController 以字母顺序以外的顺序显示部分。

但是您可以使用变通方法。向您的实体添加额外的、持久的整数类型属性(我们称之为 sectionOrder)。根据 states 属性设置其值(因此 "Push" 为 0,"Busy" 为 1 等)您可以例如,在 awakeFromInsert 方法中执行此操作。

然后使用 @"sectionOrder" 作为 sectionNameKeyPath 和排序描述符中的键路径。您可以使用此 UITableViewDataSource 方法将部分标题设置为“Push”、“Busy”等:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
return [sectionInfo.objects.firstObject name];
}

致谢excellent answer .

关于ios - NSSortDescriptor 和 NSFetchedResultsController 的自定义选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27969763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com