本文介绍了NSPredicate和CoreData--决定Date属性是否为“today”。 (或在昨晚12am到今晚12am之间)在iOS上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有一个 NSDate 对象保存到标记为startTime的Date属性。然后,我试图通过使用 NSPredicate 只是拉下今天的数据,如下所示:

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

我得到零结果。我理解这是因为 NSDate 对象只保留秒或毫秒或从1970年1月1日以来的任何数量?所以比较1111111111111和1111111111112,而在同一天,是两个不同的 NSDate 对象不相等。



那么,如何将 NSPredicate 格式化,这样它会吗?我要猜测它创建了两个 NSDate 对象:一个是在昨晚12点,另一个是今天晚上12点,比较startDate,看看它是否在这两个 NSDate s。



我是一个新的 NSPredicate

使用 NSCompoundPredicate 。 / p>

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

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

其中 firstDate code> secondDate 今晚12am。



PS以下是如何在今天早上12点前取消:

  NSCalendar * calendar = [NSCalendar calendar]; //获取默认日历
NSCalendarComponents * components = [日历组件:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)fromDate:[NSDate date]]; //获取当前日期的年,月和日
NSDate * firstDate = [calendar dateFromComponents:components]; //使一个新的NSDate仅保留年,月和日

components.day


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?

解决方案

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.

这篇关于NSPredicate和CoreData--决定Date属性是否为“today”。 (或在昨晚12am到今晚12am之间)在iOS上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 18:24