天循环中的整数在所有天中继续重复
Ints in loop of days continues to repeat for all days
我已经尝试多次更改我的函数,使 NSDates 的 day/end 开始,我知道循环正在工作,因为它打印出一周中的正确日期,但是 start_timestamp 和 end_timestamp 由于某种原因,每天都显示相同的内容。它必须是涉及 int 不改变的东西或者与我没有看到的逻辑没有直接关系的东西:
这是打印出来的内容:
WEEK_ARRAY:{
Friday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Monday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Saturday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Sunday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Thursday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Tuesday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Wednesday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
}
逻辑如下:
- (NSMutableDictionary *)lastSevenDays {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
NSDate *date = [NSDate date];
NSMutableDictionary *weekDays = [[NSMutableDictionary alloc] init];
for (int i = 0; i <7; i++) {
NSString *weekDay = [formatter stringFromDate:date];
date = [self dateByAddingOneDayFromDate:date];
NSMutableDictionary *specificDayDict=[[NSMutableDictionary alloc]init];
NSDate *StartNSDate=[self beginningOfDay:date];
NSNumber *StartTstamp=[NSNumber numberWithInt:[self convertNSDateToTimestamp:StartNSDate]];
int endOfDay=[self convertNSDateToTimestamp:[self endOfDay:date]];
NSLog(@"DAY:%@ | DAY_START:%@ | DAY_END:%d",weekDay,StartTstamp,endOfDay);
[specificDayDict setValue:StartTstamp forKey:@"DAY_START"];
[specificDayDict setValue:[NSNumber numberWithInt:endOfDay] forKey:@"DAY_END"];
[weekDays setObject:specificDayDict forKey:weekDay];
}
return weekDays;
}
- (NSDate *)dateByAddingOneDayFromDate:(NSDate *)date {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *plusOneDay = [[NSDateComponents alloc] init];
[plusOneDay setDay:+1];
NSDate *newDate = [cal dateByAddingComponents:plusOneDay
toDate:date
options:NSWrapCalendarComponents];
return newDate;
}
-(NSDate *)beginningOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
}
-(NSDate *)convertTimestampToNSDate:(int)timestamp{
NSDate* date = [NSDate dateWithTimeIntervalSince1970:timestamp];
return date;
}
-(int )convertNSDateToTimestamp:(NSDate *)date{
int timestamp=[date timeIntervalSince1970];
return timestamp;
}
原因是 beginningOfDay
和 endOfDay
方法中都缺少 NSDayCalendarUnit
组件。
在 beginningOfDay 和 endOfDay 函数中,您错过了 NSCalendarUnitDay。 ios8.
中也弃用了 NSMonthCalendarUnit、NSYearCalendarUnit
-(NSDate *)beginningOfDay:(NSDate *)date{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay| NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay| NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
}
我已经尝试多次更改我的函数,使 NSDates 的 day/end 开始,我知道循环正在工作,因为它打印出一周中的正确日期,但是 start_timestamp 和 end_timestamp 由于某种原因,每天都显示相同的内容。它必须是涉及 int 不改变的东西或者与我没有看到的逻辑没有直接关系的东西:
这是打印出来的内容:
WEEK_ARRAY:{
Friday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Monday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Saturday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Sunday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Thursday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Tuesday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Wednesday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
}
逻辑如下:
- (NSMutableDictionary *)lastSevenDays {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
NSDate *date = [NSDate date];
NSMutableDictionary *weekDays = [[NSMutableDictionary alloc] init];
for (int i = 0; i <7; i++) {
NSString *weekDay = [formatter stringFromDate:date];
date = [self dateByAddingOneDayFromDate:date];
NSMutableDictionary *specificDayDict=[[NSMutableDictionary alloc]init];
NSDate *StartNSDate=[self beginningOfDay:date];
NSNumber *StartTstamp=[NSNumber numberWithInt:[self convertNSDateToTimestamp:StartNSDate]];
int endOfDay=[self convertNSDateToTimestamp:[self endOfDay:date]];
NSLog(@"DAY:%@ | DAY_START:%@ | DAY_END:%d",weekDay,StartTstamp,endOfDay);
[specificDayDict setValue:StartTstamp forKey:@"DAY_START"];
[specificDayDict setValue:[NSNumber numberWithInt:endOfDay] forKey:@"DAY_END"];
[weekDays setObject:specificDayDict forKey:weekDay];
}
return weekDays;
}
- (NSDate *)dateByAddingOneDayFromDate:(NSDate *)date {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *plusOneDay = [[NSDateComponents alloc] init];
[plusOneDay setDay:+1];
NSDate *newDate = [cal dateByAddingComponents:plusOneDay
toDate:date
options:NSWrapCalendarComponents];
return newDate;
}
-(NSDate *)beginningOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
}
-(NSDate *)convertTimestampToNSDate:(int)timestamp{
NSDate* date = [NSDate dateWithTimeIntervalSince1970:timestamp];
return date;
}
-(int )convertNSDateToTimestamp:(NSDate *)date{
int timestamp=[date timeIntervalSince1970];
return timestamp;
}
原因是 beginningOfDay
和 endOfDay
方法中都缺少 NSDayCalendarUnit
组件。
在 beginningOfDay 和 endOfDay 函数中,您错过了 NSCalendarUnitDay。 ios8.
中也弃用了 NSMonthCalendarUnit、NSYearCalendarUnit-(NSDate *)beginningOfDay:(NSDate *)date{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay| NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay| NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
}