我有一些代码用于排序日历日期,如下所示:
#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:
代码有什么问题。 任何帮助,将不胜感激。
运行Xcode V 4.5 btw
更新:
回溯:
0 CoreFoundation 0x39ff0e55 + 84
1 APPNAME 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
以便在国际上工作。
I have some code I use to sort calendar dates that looks like this:If I run it in the simulator all is ok.If I run it on the device I get this sarcastic debug message.2012-09-19 22:40:13.972 APPNAME [4923:907] * -[__NSCFCalendar components:fromDate:]: date cannot be nilI mean really, what do you think that operation is supposed to mean with a nil date?An exception has been avoided for now.A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil.Here is the backtrace where this occurred this time (some frames may be missing due to compiler optimizations):You have to laugh at it but I’m not sure what is wrong with my dateFormatFromTemplate:
code.Any help would be appreciated.Running Xcode V 4.5 btwUPDATE:Backtrace:0 CoreFoundation 0x39ff0e55 + 841 APPNAME 0x00040be1 -[MeetingsViewController dateAtBeginningOfDayForDate:] + 140So I guess things are going bad in my dateAtBeginningOfDayForDate
method.Which looks like this:I use this method to break down a given NSDate to its’ core components so that I can sort them all more efficiently.However, my app has to be international which is the reason behind using NSLocale
to formate the date output.From what it seems I will need to alter my dateAtBeginningOfDayForDate
to work internationally as well.