日期选择器代码有问题
我最近在我的医疗应用程序中添加了一项功能,可以根据输入的日期计算患者的年龄。如果年龄为零,则日期选择器将显示 1950 年 1 月 1 日作为输入出生日期的默认开始日期。该代码在 iPhone 和 iPad 硬件上的 iOS 4.0+ 上运行良好。然而,在提交应用程序更新之前进行测试时,我发现日期选择器在我的第一代 iPod 上崩溃了,我保留它来测试 iOS 3.1.3。我缩小了以下代码作为罪魁祸首的范围,它设置了日期选择器的默认日期:
if (age == 0) {
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"M/d/yy"];
NSDate *initialDate = [myFormatter dateFromString:@"January/01/1950"];
[picker setDate:initialDate animated:YES];
}
iPod 的日志在崩溃后显示以下两行:
<Warning>: *** Assertion failure in -[UIDatePickerView _updateBitsForDate:andReload:animateIfNeeded:], /SourceCache/UIKit/UIKit-984.38/UIDatePicker.m:908
如果
<Error>: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
您能帮助我理解为什么此崩溃仅发生在较旧的设备上,我将不胜感激设备,以及如何解决该问题。提前致谢。
I recently added a feature to my medical app that calculates a patient's age from an entered date. If the age is zero, then the date picker displays January 1, 1950 as a default starting date for entry of the date of birth. The code works fine on iOS 4.0+ on iPhone and iPad hardware. However, when testing just prior to submission for an app update, I found the date picker crashing on my first-generation iPod, which I keep for testing iOS 3.1.3. I narrowed down the following code as the culprit, which sets the default date for the date picker:
if (age == 0) {
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"M/d/yy"];
NSDate *initialDate = [myFormatter dateFromString:@"January/01/1950"];
[picker setDate:initialDate animated:YES];
}
The log from the iPod shows the following two lines after the crash:
<Warning>: *** Assertion failure in -[UIDatePickerView _updateBitsForDate:andReload:animateIfNeeded:], /SourceCache/UIKit/UIKit-984.38/UIDatePicker.m:908
and
<Error>: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
I would appreciate any help in understanding why this crash occurs only on the older device, and how to solve the issue. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 tr35-10 标准,您的日期格式应为
MMMM/dd/yyyy
。您还应该 缓存格式化程序以提高效率。
According to the tr35-10 standard, your date format should be
MMMM/dd/yyyy
.You should also cache formatters for efficiency.