Created
March 19, 2012 20:57
-
-
Save MrRooni/2127049 to your computer and use it in GitHub Desktop.
How not to increment an NSDate, corrected!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
(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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is that you can’t use the
performSelector:withObject:
method when the parameter is not actually an object.