简体   繁体   English

无法在if语句中比较NSDates

[英]Can't compare NSDates in if statement

I am fetching data from iCloud and would like to compare it if it's more recent than the data on the device. 我正在从iCloud提取数据,如果它比设备上的数据更新,我想对其进行比较。 However, my if statement evaluates to true even if the iCloud data is older. 但是,即使iCloud数据较旧,我的if语句也会计算为true。

Here is my code: 这是我的代码:

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);
        }

Console: 安慰:

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

As you can see clearly, iCloud data is older, and the switch statement says that the order is ascending, however the if statement still evaluates to true. 正如您可以清楚地看到的那样,iCloud数据较旧,并且switch语句说顺序是递增的,但是if语句仍为true。 I even checked to make sure that both dates are not nil before comparing. 在比较之前,我什至检查以确保两个日期都不nil

I have been struggling with this for an hour, can't seem to figure out what's going on.. probably some obvious thing which I am overlooking. 我已经为此苦苦挣扎了一个小时,似乎无法弄清楚发生了什么……可能是我忽略了一些明显的事情。

Thanks in advance! 提前致谢!

EDIT : No matter what I compare result with, it always evalutes to true 编辑 :无论我与result进行比较,它总是评估为true

When I run this code: 当我运行此代码时:

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");
}

I get this result: 我得到这个结果:

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

I can't run your code unless you post either the whole project, or a cut-down example which demonstrates the problem. 除非您发布整个项目或一个演示问题的简化示例,否则我无法运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM