我做了类似的事情,我在名为 的实体中使用了一个额外的属性sortDate
。我从我的自定义日期设置器中设置了 sortDate。
像这样的东西:
- (NSDate *)monthDateFromDate:(NSDate *)date {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit fromDate:date];
NSDate *monthDate = [calendar dateFromComponents:components];
return monthDate;
}
- (void)setDate:(NSDate *)date {
[self willChangeValueForKey:@"date"];
[self setPrimitiveStartDate:date];
[self didChangeValueForKey:@"date"];
NSDate *sortDate = [self monthDateFromDate:date];
[self willChangeValueForKey:@"sortDate"];
[self setPrimitiveSortDate:sortDate];
[self didChangeValueForKey:@"sortDate"];
}