ran*_*dom 83 debugging objective-c nsdateformatter ios
我有一些代码用于排序日历日期,如下所示:
#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
Run Code Online (Sandbox Code Playgroud)
如果我在模拟器中运行它就可以了.如果我在设备上运行它,我会得到这个讽刺调试消息.
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;
}
Run Code Online (Sandbox Code Playgroud)
我使用这种方法将给定的NSDate分解为其核心组件,以便我可以更有效地对它们进行排序.但是,我的应用程序必须是国际的,这是使用NSLocale合成日期输出的原因.从我看来,我需要改变我的dateAtBeginningOfDayForDate国际工作.
Ana*_*ile 16
您发布的代码中不会发生此错误.这意味着某些地方你应该有一些东西,NSDate而不是它nil.
至于错误信息中的幽默,我从来没有见过这个消息,但是+1给Apple.可可部门的一些工程师和苹果过去很久以前的优秀老工程师一样有趣.
查看Apple的MPW C编译器用于吐出的诊断类型http://www.netfunny.com/rhf/jokes/91q3/cerrors.html
非常感谢nneonneo帮助我解决这个问题.
我的问题是我的应用程序必须在国际上工作,所以我需要显示与用户位置有关的日期.我以为我只是创建一个NSDateFormatter用户区域设置然后传递一个字符串日期,如下所示:
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0
locale:[NSLocale currentLocale]];
date = [fmt dateFromString:[NSString stringWithFormat:@"%@ %@", obj.meetingTime, obj.meetingDate]];
Run Code Online (Sandbox Code Playgroud)
但是,问题是我在传递字符串日期之前是本地化的日期格式.在我的情况下NSDateFormatter,本地化后看起来像这样.
MMM dd, yyyy, HH:mm
Run Code Online (Sandbox Code Playgroud)
在本地化后,本地化之后的格式可能会有很多不同的方式.
所以传入一个格式为的字符串HH:mm dd MMM yyyy导致它返回nil.
现在哪个很明显 -.-
因此,我在创建日期时不是本地化,而是使用标准格式创建日期HH:mm dd MMM yyyy.然后在显示时,我将格式化NSDate为用户各自的语言环境.
| 归档时间: |
|
| 查看次数: |
17260 次 |
| 最近记录: |