NSPredicate 和 CoreData - 决定日期属性是否为“今天” (或昨晚 12 点到今晚 12 点之间)在 iOS 上

发布于 2025-01-08 12:39:39 字数 708 浏览 4 评论 0原文

我使用 NSFetchedResultsControllerUITableViewController 从 CoreData 数据库填充 UITableView。

我有一个 NSDate 对象保存到标记为“startTime”的日期属性中。然后,我尝试使用如下所示的 NSPredicate 来提取今天的数据:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startDate == %@",
                          todaysDate];

我得到的结果为零。我理解这一点是因为 NSDate 对象只保存自 1970 年 1 月 1 日以来的秒数或毫秒数,对吧?因此,在同一天比较 1111111111111 和 1111111111112 时,两个不同的 NSDate 对象不相等。

那么,如何格式化 NSPredicate 才能执行此操作呢?我猜测它会创建两个 NSDate 对象:一个是昨晚 12 点,另一个是今晚 12 点,然后比较 startDate,看看它是否在这两个 NSDate 之间s。

我对 NSPredicate 有点陌生,那么这是如何实现的呢?

I'm using a NSFetchedResultsController and a UITableViewController to populate a UITableView from a CoreData database.

I have a NSDate object saved into this Date attribute labeled "startTime". Then I'm trying to only pull todays's data by using a NSPredicate that looks like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startDate == %@",
                          todaysDate];

I'm getting zero results. I understand this because a NSDate object just hold the number of seconds or milliseconds or whatever since Jan 1 1970 right? So comparing 1111111111111 and 1111111111112, while on the same day, are two distinct NSDate objects that aren't equal.

So, how could the NSPredicate be formatted so that it would do it? I'm going to guess its creating two NSDate objects: one that is at 12am last night, and another for 12am tonight and compare startDate and see if its between these two NSDates.

I'm kind of new to NSPredicate, so how could this be accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2025-01-15 12:39:39

使用 NSCompoundPredicate。

NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:@"startDate > %@", firstDate];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"startDate < %@", secondDate];

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:firstPredicate, secondPredicate, nil]];

其中,firstDate 是今天早上 12 点,secondDate 是今晚 12 点。

PS 以下是获取今天早上 12 点的方法:

NSCalendar *calendar = [NSCalendar calendar]; // gets default calendar
NSCalendarComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, and day for today's date
NSDate *firstDate = [calendar dateFromComponents:components]; // makes a new NSDate keeping only the year, month, and day

要获取今晚 12 点,请更改 components.day

Use NSCompoundPredicate.

NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:@"startDate > %@", firstDate];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"startDate < %@", secondDate];

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:firstPredicate, secondPredicate, nil]];

Where firstDate is 12am this morning and secondDate is 12am tonight.

P.S. Here's how to get 12am this morning:

NSCalendar *calendar = [NSCalendar calendar]; // gets default calendar
NSCalendarComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, and day for today's date
NSDate *firstDate = [calendar dateFromComponents:components]; // makes a new NSDate keeping only the year, month, and day

To get 12am tonight, change components.day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文