确定NSPredicate中是否存在NSDate [英] Determining if an NSDate is today in NSPredicate
本文介绍了确定NSPredicate中是否存在NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在该站点上尝试了多种方法并尝试了一些建议,但是如何确定CoreData属性 dueDate是否等于 Today,我也知道NSDate也是一个特定时间,但是我想要谓词如果是同一天/月/年,则返回该值,而不管一天中的时间。有任何建议吗?
I've tried multiple methods and tried some suggestions on this site, but how can I determine if a CoreData attribute "dueDate" is equal to "Today", I know NSDate is a specific time as well but I want the predicate to return if it is the same day/month/year regardless of what the time is in the day. Any suggestions?
推荐答案
这是我想出的:
- (NSPredicate *)matchAttribute:(NSString *)attributeName withDate:(NSDate *)date
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
oneDay.day = 1;
NSDate *lastMidnight = [[NSCalendar currentCalendar] dateFromComponents:components];
NSDate *nextMidnight = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay toDate:lastMidnight options:NSWrapCalendarComponents];
return [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K <= %@)", attributeName, lastMidnight, attributeName, nextMidnight];
}
这篇关于确定NSPredicate中是否存在NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文