ios app在iOS 8中返回hijri日历年

时间:2014-08-27 11:50:12

标签: ios objective-c ios8

我有一个iOS应用程序,它在iOS 7上运行良好。

我从1970年到今年生成了列车模型。继承人的代码

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

NSInteger currentYear = [components year];
DLog(@"current year = %ld", (long)currentYear)

for (NSInteger yearStart = (currentYear + 1); yearStart >= kHJVehicleStartingModelYear ; yearStart--) {
    [self.carModels addObject:[NSString stringWithFormat:@"%ld", (long)yearStart]];
}

问题是在iOS 8中运行时。根据区域设置,它返回Hijri日历的当前年份。例如它返回1435而不是2014.并且循环不会执行以生成模型列表。

如何在iOS 8中获得格里高利当年?

1 个答案:

答案 0 :(得分:0)

您只需使用NSGregorianCalendar类型创建NSCalendar的实例:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];