当前位置: 代码迷 >> Iphone >> IPHONE MinutesToMidnight 出现的编译异常,已找到解决办法
  详细解决方案

IPHONE MinutesToMidnight 出现的编译异常,已找到解决办法

热度:73   发布时间:2016-04-25 06:27:02.0
IPHONE MinutesToMidnight 出现的编译错误,已找到解决方法
-(void)updateLabel {
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];



修改为:

-(void)updateLabel {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit| NSMinuteCalendarUnit |NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];
int hour = 23 - [comps hour];
int min = 59 - [comps minute];
int sec = 59 - [comps second];
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
}

  相关解决方案