- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用 NSFetchedResultsController 和 NSSortDescriptor 成功地排序到一个表中,每个日期都有一个部分,因此今天的日期首先出现(日期降序)。
但是,在该部分中,我希望时间按升序排序。
这是我当前的代码,没有按时间排序:
//Set up the fetched results controller.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:kEntityHistory inManagedObjectContext:global.managedObjectContext];
fetchRequest.entity = entity;
// Sort using the timeStamp property..
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
fetchRequest.sortDescriptors = sortDescriptors;
//The following is from:
//http://stackoverflow.com/questions/7047943/efficient-way-to-update-table-section-headers-using-core-data-entities
NSString *sortPath = @"date.dayDate";
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:global.managedObjectContext sectionNameKeyPath:sortPath cacheName:nil];
fetchedResultsController.delegate = self;
请问如何更改代码以在此日期部分添加按时间升序排序?
最佳答案
我认为你需要一个特殊的比较器。使用 initWithKey:ascending:comparator:初始化你的排序描述符并作为比较器传递这个:
^(id obj1, id obj2) {
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSDayCalendarUnit fromDate:obj1];
NSInteger day1 = [components1 day];
NSInteger hour1 = [components1 hour];
NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSDayCalendarUnit fromDate:obj2];
NSInteger day2 = [components2 day];
NSInteger hour2 = [components2 hour];
NSComparisonResult res;
if (day1>day2) {
res = NSOrderedAscending;
} else if (day1<day2) {
res = NSOrderedDescending;
} else {
if (hour1>hour2) {
res = NSOrderedDescending;
} else {
res = NSOrderedAscending;
}
}
return res;
}
这应该让您了解如何实现这一目标,您将需要添加分和秒组件,并处理时、分和秒相等的情况。
关于ios - 如何按日期降序对 NSDate 进行排序,但在该日期内时间升序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11555912/
我的应用程序中有两个 UIDatePicker 控件,一个配置了 UIDatePickerModeDate,另一个配置了 UIDatePickerModeTime。 我想配置一个 NSDate 对象,
我可以通过此代码获取 iPhone 的当前日期 NSDate *currentDate = [NSDate date]; NSDateFormatter *dateFormatter1 = [[NSD
我在 Swift 中有一个模型,它有一个名为 expirationDate 的 NSDate 字段,我想根据当前时间计算过期前的剩余小时数日期。 有没有一种简单的方法可以利用现有的 NSDate 功能
我将我的 NSDate 存储到一个文件中,该文件采用您在 NSDate 上调用描述时获得的国际格式。 但是,我不知道如何从字符串格式返回到 NSDate 对象。 NSDateFormatter 似乎仅
我有兴趣创建一种方法来查找当前日期是否落在任何给定日期的特定时间之间。这是一个调度程序,所以我想找到当前时间正在发生什么事件。每天都有相同的时间集:820-900、900-940、940-1020 等
我有一个约会时间: Friday, July 8, 2016 at 6:30:00 PM India Standard Time 我想要这样的约会时间: Friday, July 8, 2016 at
我有一个 NSDate 对象。假设它代表“1-10-2011” NSDate *date = [df dateFromString:@"2011-10-01 00:00:00"]; 由于我所在的时区,
我从 Django API 获取 NSString 2012-08-17T10:56:45.508205 作为时间。 我正在尝试使用以下代码将该字符串转换为 NSDate 对象: NSDateForm
我在获取一系列 NSDates 与另一个 NSDates 范围相交的天数时遇到问题。 场景 1: 主要范围:3月8日-6月8日 检查范围:4月8日-5月8日 那么相交的天数就是4月8日-5月8日的天数
我知道如何获取两个 NSDate 之间的差异,如下所示 NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:anyPr
我需要根据另一个 NSDate 获取一天中特定时间的 NSDate。例如,如果我有一个代表 2015 年 5 月 10 日已知时间的 NSDate,我需要能够获得一个代表 2015 年 5 月 10
升级到1.2后出现编译错误: NSDate?! is not convertible to NSDate 代码: let dateCreated = photoCommentObjects[index
这是我的代码。但是一个错误说,不能用类型的参数列表调用 dateComponent... let fromDate = NSDate(timeIntervalSince1970: TimeInter
给定一个引用日期,我需要找到下面的“星期三”。 需要说明的是,如果引用日期是星期二,那么它应该返回同一周的星期三。 如果引用日期是星期三或更晚,则需要返回从下周开始的下一个星期三。 重要的是它不会返回
我有 NSDate* day1 和 NSDate* day2。我如何通过只复制第 1 天的年/月/日和第 2 天的小时/分钟/秒来制作 NSDate* day3? 最佳答案 unsigned unit
我有两个 NSDate 对象,我想要两者之间的差异,结果应该再次是一个 NSDate 对象。知道如何实现这一目标吗? 在这里,我试图解决一个独特的问题,我必须找出耗时,然后本地化耗时。如果我在 NSD
我试图强制用户使用日期选择器选择 future 的日期。我显然使用了compare:方法,但是,当我执行以下代码时,即使它与[NSDate date]相同的日期,它也会告诉执行if语句。这是我的代码:
我有两个用户选择的日期:开始日期和结束日期。它们是 NSDate 实例,我必须将它们作为 NSNumber 的参数发送。如何以秒为单位将它们转换为 NSNumber? 最佳答案 使用下面的代码: N
我对将 NSTimeInterval 转换为 NSDate 感到困惑,我有两个日期要比较,一个来自服务器,另一个在本地, 在服务器案例中,我以时间间隔格式获取日期,因此我也必须对本地数据执行相同的操作
我有一个从服务器获取的字符串 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"我通过这个 formate 将这个字符串转换成 NSDate。 class func convertUTCDat
我是一名优秀的程序员,十分优秀!