user3804862
user3804862

Reputation: 107

iOS comparing two same NSDates does not return NSOrderedSame

As the title, I have tried the following simple code:

NSDate *date1 = [NSDate date];
NSDate *date2 = [NSDate date];
if([date1 compare:date2] == NSOrderedSame)
 NSLog(@"two time same");
else
 NSLog(@"two time not same");

and the result is:

two time not same

I don't understand that, can someone give me some tips? Thank you.

Upvotes: 3

Views: 413

Answers (1)

Retro
Retro

Reputation: 4005

it's not same because there is a little time difference of creation of object, just try to print there value in NSTimeInterVal to know better.

Example:

NSDate *date1 = [NSDate date];
NSDate *date2 = [NSDate date];

NSLog(@"date1 : %f", [date1 timeIntervalSince1970]);
NSLog(@"date2 : %f", [date2 timeIntervalSince1970]);

Output:

date1 : 1404466511.638555
date2 : 1404466511.638558

and you also can see the difference using method

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate

Upvotes: 9

Related Questions