gpt4 book ai didi

objective-c - 使用 NSFetchedResultsController 的 NSSortDescriptor 进行数字排序

转载 作者:行者123 更新时间:2023-11-29 05:06:51 25 4
gpt4 key购买 nike

我正在尝试对 UITableView 中显示的数据进行数字排序。

之前我使用过这样的排序描述符:

sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

现在我想使用 block 来按数字排序,如下所示:

sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
return [((NSString *)obj1) compare:(NSString *)obj2 options:NSCaseInsensitiveSearch | NSNumericSearch];
}];

但它对数据的排序不正确,导致与 NSFetchedResultsController 中的节名称发生冲突。因此,我尝试使用比较器 block 来模仿旧的排序 - 只是为了确保问题不是由数字比较引起的。问题是这些行:

sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
return [((NSString *)obj1) caseInsensitiveCompare:(NSString *)obj2];
}];

也会导致同样的错误,我不明白为什么他们不会像第一个方法那样对数据进行排序...

有什么想法吗?

最佳答案

我不知道这是否适用于获取请求,但您可以尝试按 @selector(localizedStandardCompare:) 排序

关于objective-c - 使用 NSFetchedResultsController 的 NSSortDescriptor 进行数字排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4690875/

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