比较两个NSDate对象不工作 [英] comparing Two NSDate objects not working

查看:115
本文介绍了比较两个NSDate对象不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何将 NSDate 对象与 isEqualToDateDate 方法进行比较。我不明白为什么这个简单的测试不返回true并打印出 NSLog 。提前感谢

I'm learning about how to compare NSDate objects with the isEqualToDateDate method. I don't get why this simple test does not return true and print out the NSLog. Thanks in advance

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSDate *myDate =    [[NSDate alloc]initWithTimeIntervalSinceNow:4000000];
    NSDate *otherDate = [[NSDate alloc]initWithTimeIntervalSinceNow:4000000];

     NSLog(@"myDate    %@",myDate);
     NSLog(@"otherDate %@",otherDate);

    if ([myDate isEqualToDate:otherDate]) {

        NSLog(@"The dates are the same");
    };

    [myDate release];
    [otherDate release];
    [pool drain];
    return 0;
}


推荐答案

是错误的,因为你正在使用 initWithTimeIntervalSinceNow 分配的对象在稍微不同的时间,因此使它们不相等。

I believe, and this may be wrong, that since you are using initWithTimeIntervalSinceNow that the objects are being allocated at slightly different times, thus making them unequal.

这篇关于比较两个NSDate对象不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆