问题
From my table I want select all rows of a particular date without considering the time component of NSDate.Please any one suggest a NSPredicate to achieve this ?
回答1:
NSDate
describes an absolute point in time and does not have "day" or "time" components.
The same is true for a Core Data "Date" attribute, which is stored as the number of seconds
since 1 January 2001, GMT in the database.
What you probably want is to fetch all objects where some "Date" attribute falls into the
interval given by the start of a given day and the start of the next day.
Therefore you have to compute these dates first, using NSCalendar
methods:
NSDate *yourDate = ...;
NSDate *startOfDay, *startOfNextDay;
NSTimeInterval lengthOfDay;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit startDate:&startOfDay interval:&lengthOfDay forDate:yourDate];
startOfNextDay = [startOfDay dateByAddingTimeInterval:lengthOfDay];
Then use a predicate that fetches all objects where the attribute falls between these two values:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date >= %@ AND date < %@",
startOfDay, startOfNextDay];
来源:https://stackoverflow.com/questions/24186284/nspredicate-without-consider-time-component