我有点难理解为什么以下代码会导致我的应用程序崩溃:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM d, yyyy"];
NSDate *date = [dateFormatter dateFromString:cDate];
datePicker.date = date;
NSString *dateStr = [dateFormatter stringFromDate:date];
[dateLabel setText:dateStr];
[dateFormatter release];
如果我将上面的内容注释掉,应用程序就可以正常运行。如果我将日期格式更改为以下格式,也不会发生崩溃:
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
在我的
UIDatePicker
代理中,我有重复的代码,如下所示(并且非常有效):-(IBAction)datePickerValueChanged:(id)sender
{
NSDate *date = [datePicker date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MMMM d, yyyy"];
NSString *dateStr = [dateFormatter stringFromDate:date];
[dateLabel setText:dateStr];
}
我收到的错误信息如下:
*** Assertion failure in -[UIDatePickerView _updateBitsForDate:andReload:animateIfNeeded:], /SourceCache/UIKit/UIKit-747.38/UIDatePicker.m:892
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
setDate:
或setDate:animated:
的NSDate
实例实际上是nil
时,会抛出该断言。 - Dave DeLong