使用 NSDate 和 company 计算日期有点复杂,但一旦你掌握了它,它实际上非常强大。这是过滤数组的一种方法:
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSMutableArray *dates = [NSMutableArray array];
NSDate *selectedDate = [NSDate date];
for (int i=0; i<70; i++)
[dates addObject:[NSDate dateWithTimeIntervalSinceNow:i * 60*60*24]];
NSInteger selectedDayOfWeek = [calendar components:NSWeekdayCalendarUnit fromDate:selectedDate].weekday;
NSPredicate *filter = [NSPredicate predicateWithBlock:^(id object, NSDictionary *bindings) {
return (BOOL)([calendar components:NSWeekdayCalendarUnit fromDate:object].weekday == selectedDayOfWeek);
}];
NSArray *filteredDates = [dates filteredArrayUsingPredicate:filter];
NSArray *sortedDates = [filteredArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", filteredDates);
}
return 0;
}