tags:

views:

47

answers:

1

I am trying to work through some of the AppsAmuck tutorials but they mostly seem to use code that Apple has deprecated. For example, there is a function that is called by a timer every second that looks like this:

NSDate *now = [NSDate date];
    int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
    int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];

Unfortunately, none of this works with today's current SDK. I have been staring glossy-eyed at Apple's NSDate documentation and I am having trouble translating the intent of the function above into current day NSDate lingo.

A: 

This is what I use...but there are other methods

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[gregorian release];

Returns the hour, minute and second from the current

Jordan