这个iOS有什么讽刺的错误? [英] What kind of sarcastic error is this iOS?
问题描述
我有一些代码来排序日历日期,如下所示:
I have some code I use to sort calendar dates that looks like this:
#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
如果我在模拟器中运行它,都可以。如果我在设备上运行它,我得到这个讽刺的调试消息。
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
组件:fromDate:]:date不能为零
2012-09-19 22:40:13.972 APPNAME [4923:907] * -[__NSCFCalendar components:fromDate:]: date cannot be nil
我的意思是真的,你有什么
认为这个操作应该是一个零日期?
I 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):
你必须笑,但我不知道我的 dateFormatFromTemplate:
代码有什么问题。任何帮助将不胜感激。
You have to laugh at it but I'm not sure what is wrong with my dateFormatFromTemplate:
code. Any help would be appreciated.
运行Xcode V 4.5 btw
更新:
Backtrace:
Backtrace:
0 CoreFoundation 0x39ff0e55 + 84 < br>
1
APPNAME 0x00040be1
- [MeetingsViewController dateAtBeginningOfDayForDate:] + 140
0 CoreFoundation 0x39ff0e55 + 84
1
APPNAME 0x00040be1
-[MeetingsViewController dateAtBeginningOfDayForDate:] + 140
所以我猜测我的 dateAtBeginningOfDayForDate
方法中的事情会变坏。看起来像这样:
So I guess things are going bad in my dateAtBeginningOfDayForDate
method. Which looks like this:
/*
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 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.
推荐答案
非常感谢 nneonneo 帮助我弄清楚。
Much thanks to nneonneo for helping me figure this out.
我的问题是我的应用程序必须在国际上工作,所以我需要相对于用户位置显示的日期。我以为我会创建一个 NSDateFormatter
给用户区域设置,然后传递一个字符串日期,如下所示:
My issue is that my app has to work internationally so I needed dates to display with respect to the user location. I thought that I would just create a NSDateFormatter
to the users locale then pass in a string date like so:
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0
locale:[NSLocale currentLocale]];
date = [fmt dateFromString:[NSString stringWithFormat:@"%@ %@", obj.meetingTime, obj.meetingDate]];
但是,问题是我在之前本地化了日期格式在字符串日期传递。在我的情况下,在本地化之后 NSDateFormatter 看起来像这样。
However, the problem is that I was localization the date format before I passed in the string date. In my case NSDateFormatter
looked like this after localizing.
MMM dd, yyyy, HH:mm
我本地化后的格式在本地化后可能会有许多不同的方式。
Which I the format after localization could look many different ways after localizing.
所以传递一个格式为 HH:mm dd MMM yyyy
的字符串导致它返回 nil
。
So passing in a string with a format of HH:mm dd MMM yyyy
was causing it to return nil
.
现在很明显 -.-
所以,而不是本地化创建日期,我创建一个标准格式为 HH:mm dd MMM yyyy
的日期。然后在显示时,将 NSDate
格式化为用户各自的区域设置。
So instead of localizing when I create the date, I create a the dates with a standard format of HH:mm dd MMM yyyy
. Then when displaying, I format the NSDate
to the users respective locale.
这篇关于这个iOS有什么讽刺的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!