Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 比较两个不工作的NSDate对象_Objective C_Cocoa - Fatal编程技术网

Objective c 比较两个不工作的NSDate对象

Objective c 比较两个不工作的NSDate对象,objective-c,cocoa,Objective C,Cocoa,我正在学习如何使用isequaldatedate方法比较NSDate对象。我不明白为什么这个简单的测试不返回true并打印出NSLog。提前谢谢 #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSDate *myDate = [[

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

#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;
}
#导入
int main(int argc,const char*argv[]
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
NSDate*myDate=[[NSDate alloc]initWithTimeIntervalSinceNow:4000000];
NSDate*otherDate=[[NSDate alloc]initWithTimeIntervalSinceNow:4000000];
NSLog(@“myDate%@”,myDate);
NSLog(@“其他日期%@”,其他日期);
如果([myDate isEqualToDate:otherDate]){
NSLog(“日期相同”);
};
[我的发布日期];
[其他日期发布];
[泳池排水沟];
返回0;
}

我相信,这可能是错误的,因为您现在使用的是
initWithTimeIntervals
,所以对象在稍微不同的时间分配,从而使它们不相等

两个日期确实略有不同。显示差异的快速示例:

NSDate *one = [[NSDate alloc]initWithTimeIntervalSinceNow:4000000];
NSDate *two = [[NSDate alloc]initWithTimeIntervalSinceNow:4000000];

NSComparisonResult difference = [two compare:one];

NSLog(@"Date one: %@",one);
NSLog(@"Date two: %@",two);

NSLog(@"Exact difference: %ld",difference);
输出:

Date one: 2012-01-03 07:47:40 +0000
Date two: 2012-01-03 07:47:40 +0000
Exact difference: 1
Equal
编辑

isEqualToDate:
在以下示例中返回
true

NSDate *one = [[NSDate alloc]initWithTimeIntervalSince1970:4000000];
NSDate *two = [[NSDate alloc]initWithTimeIntervalSince1970:4000000];
if ([one isEqualToDate:two]) NSLog(@"Equal");
输出:

Date one: 2012-01-03 07:47:40 +0000
Date two: 2012-01-03 07:47:40 +0000
Exact difference: 1
Equal

是的,我想你可能是对的-我从1970年起将方法更改为initWithTimeIntervals,一切都很好。我不明白的是,如果这个方法总是返回false,那么它是做什么的。任何进一步的帮助都会很好。查看1意味着它是由Hanks Anne订购的,但是有什么不同呢?他们打印的是相同的!!它们在毫秒上是不同的,而在微妙的毫秒上是不同的好的,那么这个方法有什么意义呢?如果它返回一个BOOL,那么它将始终为false!当然在处理时间问题时,你必须定义比较的准确度。