You could always get the date as string using NSDateFormatter
(or simple description
method) and do a check for particular string. But for this you will have to get all objects from your store which will be very inefficient.
Instead you could create another relationship (or custom attribute) to a Date
entity which will have day, month and year information. (You can have this along with the date attribute or without it). Then it will be easy to write predicates whatever may be the type of search you are trying to perform - month, date or day.
And for setting and getting the attribute you could use custom methods in the NSManagedObject
subclass for the entity.
For eg: if your entity is Event
which looks like
Event
- eName
- eDate (Another Entity EventDate- one to one relationship)
and EventDate
looks like
EventDate
then for setting using date
-(void)setEDateUsingDate:(NSDate *)date{
//gather current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
//gather date components from date
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
//set date components
self.eDate.day=[dateComponents day];
self.eDate.month=[dateComponents month];
self.eDate.year=[dateComponents year];
}
and getting the date would be
-(NSDate *)dateFromEDate{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:self.eDate.day];
[components setMonth:self.eDate.month];
[components setMonth:self.eDate.year];
NSDate *date = [calendar dateFromComponents:components];
return date;
}