我有一些用于对日历日期进行排序的代码,如下所示:
#if !(TARGET_IPHONE_SIMULATOR)
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0
locale:[NSLocale currentLocale]];
[fmt setDateFormat:formatString];
#else
[fmt setDateFormat:@"HH:mm dd MMM yyyy"];
#endif
如果我在模拟器中运行它一切正常。如果我在设备上运行它,我会收到这条讽刺性的调试消息。
2012-09-19 22:40:13.972 应用程序名称 [4923:907] *-[__NSCF日历
组件:fromDate:]:日期不能为零
我的意思是真的,你做什么
认为该操作应该意味着零日期?
目前已避免出现异常。
其中一些错误将会是
随此投诉一起举报,然后进一步的违规行为将只是
默默地做任何由 nil 产生的随机事情。这里是
回溯这次发生的情况(某些帧可能丢失
由于编译器优化):
你必须嘲笑它,但我不确定我的问题出在哪里dateFormatFromTemplate:
代码。任何帮助,将不胜感激。
顺便说一句,运行 Xcode V 4.5
UPDATE:
回溯:
0 核心基础 0x39ff0e55 + 84
1
应用程序名称 0x00040be1
-[MeetingsViewController 日期AtBeginningOfDayForDate:] + 140
所以我想我的事情会变得很糟糕dateAtBeginningOfDayForDate
方法。看起来像这样:
/*
Break down a given NSDate to its bare components for sorting
*/
- (NSDate *)dateAtBeginningOfDayForDate:(NSDate *)inputDate
{
// Use the user's current calendar and time zone
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[calendar setTimeZone:timeZone];
// Selectively convert the date components (year, month, day) of the input date
NSDateComponents *dateComps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:inputDate];
// Set the time components manually
[dateComps setHour:0];
[dateComps setMinute:0];
[dateComps setSecond:0];
// Convert back
NSDate *beginningOfDay = [calendar dateFromComponents:dateComps];
return beginningOfDay;
}
我使用这种方法将给定的 NSDate 分解为其核心组件,以便我可以更有效地对它们进行排序。但是,我的应用程序必须是国际化的,这就是使用的原因NSLocale
格式化日期输出。从看来我需要改变我的dateAtBeginningOfDayForDate
也可以在国际上工作。