我正在尝试从 AWS 计算一周中每个日期的数据。一周的第一天设置为星期一。这会导致我的程序在无限循环中运行。似乎无法理解为什么。
-(void)weekSectionSorting{
NSCalendar* calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Singapore"]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // Initialize NSDateFormatter.
NSDate *today = [NSDate date];
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:today];
[comps setWeekday:2]; // 2: monday
NSDateComponents* todayDc = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:today];
NSDateComponents* oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
[dateFormatter setDateFormat: @"dd-MM-yyyy"];
BOOL notToday = YES;
int count = 0;
NSDate *d = [calendar dateFromComponents:comps];
DLog(@"First day of week : %@", [dateFormatter stringFromDate:d]);
while (notToday) {
count += [[self.dataDict objectForKey:[dateFormatter stringFromDate:d]] count];
DLog(@"%i",count);
NSDateComponents* curDc = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:d];
if ([curDc year] == [todayDc year] && [curDc month] == [todayDc month] && [curDc day] == [todayDc day]) {
notToday = NO;
}
d = [calendar dateByAddingComponents:oneDay toDate:d options:0];
}
}