我有一些代码用于对日历日期进行排序,如下所示:
#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 APPNAME [4923:907] *-[__NSCFCalendar 组件:FromDate:]:日期不能为零
我是说真的,你怎么 你认为那个手术应该意味着没有约会吗?
目前已经避免了一个例外.
其中几个错误将会是 与这一投诉一起报告,那么进一步的违规行为将只需 默默地做任何由零产生的随机的事情.这是 回溯这次发生此情况的位置(某些帧可能丢失 由于编译器优化):
你不得不嘲笑它,但我不确定我的dateFormatFromTemplate:
代码出了什么问题.任何帮助都将不胜感激.
Running Xcode V 4.5 btw个
更新:
回溯:
0 CoreFoundation 0x39ff0e55+84
1个 应用名称0x00040be1 -[MeetingsViewController dateAtBeginningOfDayForDate:]+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
,以便也能在国际上工作.