Skip to content

Instantly share code, notes, and snippets.

@MrRooni
Created March 19, 2012 20:57
Show Gist options
  • Save MrRooni/2127049 to your computer and use it in GitHub Desktop.
Save MrRooni/2127049 to your computer and use it in GitHub Desktop.
How not to increment an NSDate, corrected!
/*
(Updated) Many people reached out on Twitter and directed me towards using NSDateComponents
as the generally accepted method of incrementing a date. Thanks all!
We share a framework between MoneyWell 2.0 (Lion-only) and MoneyWell 1.x (10.5+) that needs
to increment a date variable by 1 second.
Since addTimeInterval: is deprecated in 10.7 and I hate build warnings as much as everyone
else I decided to get fancy with how I go about incrementing a date object. As it turns out
I out-fancied myself and the end result of the method calls is that aDate is never incremented.
Is there another way to get an incremented date here that avoids deprecation warnings?
*/
NSDate *aDate = [NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setSecond:1];
aDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:aDate options:0];
[components release], components = nil;
@0xced
Copy link

0xced commented Mar 19, 2012

The problem is that you can’t use the performSelector:withObject: method when the parameter is not actually an object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment