【问题标题】:Ints in loop of days continues to repeat for all days天循环中的整数继续重复所有天
【发布时间】:2016-05-15 02:55:56
【问题描述】:

我尝试多次更改获取一天开始/一天结束 NSDates 的函数,并且我知道循环正在工作,因为它打印出一周中的正确日期,但是每天的 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: 方法?
  • 请不要判断,现在是凌晨 3 点。我只是在寻找答案。为什么它会重复这么多?!?!
  • “不要评判”呵呵。你来错地方了。
  • 请不要delete and repost
  • 不要在凌晨 3 点写代码。如果你不那么累的话,明天整理它会比一开始写它需要更多的时间。请参阅 vadian 的帖子。

标签: ios objective-c int nsdate nsnumber


【解决方案1】:

原因是beginningOfDayendOfDay 方法中缺少NSDayCalendarUnit 组件。

【讨论】:

  • 这适用于时间戳,但现在我意识到由于某种原因这些日子不正常?看到周五、周一、周六、周日给我的日志了吗?
  • 那是因为字典在定义上是无序的。如果您想保留订单,请使用数组并将工作日添加到字典中。
【解决方案2】:

在 beginOfDay 和 endOfDay 函数中你错过了 NSCalendarUnitDay。 NSMonthCalendarUnit、NSYearCalendarUnit 在 ios8 中也被弃用了。

-(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];

}

【讨论】:

  • 这与其他答案没有太大不同。您应该在其他答案中添加关于弃用的评论。
猜你喜欢
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多