我尝试了多种方法并尝试了此网站上的一些建议,但是如何确定 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 https://stackoverflow.com/questions/8364495/nspredicate-for-finding-events-that-occur-between-a-certain-date-range, here http://zdam.posterous.com/core-data-fetch-by-date and here https://stackoverflow.com/questions/1965331/nspredicate-filtering-objects-by-day-of-nsdate-property.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)