我已经尝试了多种方法,并在这个网站上尝试了一些建议,但是如何确定CoreData属性“dueDate”是否等于“今天”,我知道NSDate也是一个特定的时间,但我希望谓词返回无论白天是什么时间,它
- (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];
}
来自here,here和here的灵感.
