Potion Factory Blog

Quick NSDate Warning

This is a quick warning for Cocoa programmers.

The following piece of code will not trigger an assertion error:

NSDate *now = [NSDate date];

#if defined(__i386__)
NSAssert([now laterDate:nil] == now, @"now should be later than nil");
#else
NSAssert([now laterDate:nil] == nil, @"nil should be later than now");
#endif

I think that the correct return value is now since any date is more recent than no date at all, but I admit that the logic is dubious. Best thing to do is to make sure that nil does not get passed in to the date comparison methods at all.

This is the reason why Tangerine! version 1.2.3b has to exist. Don't let it get you too.

UPDATE (December 12, 2007): This is fixed in Leopard now.

Comments

Harvey Swik

I've got to ask...why are you showing an example with a #if ? One of those runs on ppc and the other on i386. And you don't say which you're running on so...

Could be a little clearer is all I'm saying.

Andy Kim

Hi Harvey,

The code snippet does not cause an assertion error regardless of the processor type because of the #if declaration. The whole point is that if you pass in nil to -[NSDate laterDate:] you get different behavior depending on the processor.