【问题标题】:Can't compare NSDates in if statement无法在 if 语句中比较 NSDates
【发布时间】:2017-01-04 21:18:32
【问题描述】:

我正在从 iCloud 获取数据,如果它比设备上的数据更新,我想比较它。但是,即使 iCloud 数据较旧,我的 if 语句也会计算为 true。

这是我的代码:

NSLog(@"users date: %@",userDefaultsModificationDate);
NSLog(@"icloud fetch date : %@",fetchedRemindersRecord.modificationDate);

NSComparisonResult result =  [[NSCalendar currentCalendar] compareDate:fetchedRemindersRecord.modificationDate toDate:userDefaultsModificationDate toUnitGranularity:NSCalendarUnitSecond];

    switch (result) {
        case NSOrderedSame:
            NSLog(@"same");
            break;

            case NSOrderedDescending:
            NSLog(@"desc");
            break;

            case NSOrderedAscending:
            NSLog(@"asc");
            break;

        default:
            break;
    }
if (result == NSOrderedDescending){

            [self createReminders:[NSKeyedUnarchiver unarchiveObjectWithData:[fetchedRemindersRecord objectForKey:@"reminders"]]];

            NSLog(@"Fetched records successfully users date: %@",userDefaultsModificationDate);
            NSLog(@"Fetched records successfully icloud fetch date : %@",fetchedRemindersRecord.modificationDate);
        }

控制台:

users date: 2016-08-28 22:52:05 +0000
icloud fetch date : 2016-08-28 22:51:57 +0000
asc
Fetched records successfully users date: 2016-08-28 22:44:16 +0000
Fetched records successfully icloud fetch date : 2016-08-28 22:43:38 +0000

您可以清楚地看到,iCloud 数据较旧,并且 switch 语句表示顺序为升序,但 if 语句仍为 true。我什至在比较之前检查以确保两个日期都不是nil

我已经为此苦苦挣扎了一个小时,似乎无法弄清楚发生了什么......可能是我忽略了一些明显的事情。

提前致谢!

编辑:无论我将result 与什么进行比较,它总是评估为真

【问题讨论】:

  • 那里没有明显的问题。但是将您的 NSLog 语句移到 if 语句之前 - 那是我们需要知道这些项目的值的时候。
  • 你的 asc 和 desc 放错地方了。
  • 是的,对不起,我在 Stackoverflow 上用手机写了代码,实际代码没问题
  • 而且您仍然没有将 NSLog 语句 放在 if 语句
  • 不幸的是,同样的事情发生了!

标签: ios xcode if-statement compare nsdate


【解决方案1】:

当我运行这段代码时:

NSDateFormatter* df = [NSDateFormatter new];
df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate* date1 = [df dateFromString:@"2016-08-28 22:52:05"];
NSDate* date2 = [df dateFromString:@"2016-08-28 22:51:57"];

NSLog(@"users date: %@", date1);
NSLog(@"icloud fetch date : %@", date2);

NSComparisonResult result =  [[NSCalendar currentCalendar] compareDate:date2 toDate:date1 toUnitGranularity:NSCalendarUnitSecond];

switch (result) {
    case NSOrderedSame:
        NSLog(@"same");
        break;

    case NSOrderedDescending:
        NSLog(@"desc");
        break;

    case NSOrderedAscending:
        NSLog(@"asc");
        break;

    default:
        break;
}
if (result == NSOrderedSame){
    NSLog(@"same 2");
}
if (result == NSOrderedDescending){
    NSLog(@"desc 2");
}
if (result == NSOrderedAscending){
    NSLog(@"asc 2");
}

我得到这个结果:

2016-08-29 09:25:22.737 Junk6[34957:1961720] users date: 2016-08-28 12:52:05 +0000
2016-08-29 09:25:22.738 Junk6[34957:1961720] icloud fetch date : 2016-08-28 12:51:57 +0000
2016-08-29 09:25:22.738 Junk6[34957:1961720] asc
2016-08-29 09:25:22.739 Junk6[34957:1961720] asc 2

除非您发布整个项目或演示问题的简化示例,否则我无法运行您的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2020-08-01
    • 2013-12-02
    • 1970-01-01
    • 2022-01-09
    • 2016-07-17
    • 1970-01-01
    相关资源
    最近更新 更多