首先,我的应用程序适用于 iOS 5 版本。尽管当从日期选择器中获取日期并尝试通过核心数据时它会抛出异常。该错误偶尔会发生。程序启动时,我在控制台中收到以下警告。
2012-10-03 10:58:20.251 MyApp[1115:c07] Application windows are expected to have a root view controller at the end of application launch
2012-10-03 10:58:20.278 MyApp[1115:c07] *** -[__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):
(
0 CoreFoundation 0x01d90a75 -[__NSCFCalendar components:fromDate:] + 85
1 MyApp 0x000034f8 -[MyApp tableView:cellForRowAtIndexPath:] + 2600
2 UIKit 0x00c9df4b -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 412
3 UIKit 0x00c9e01f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 69
4 UIKit 0x00c8680b -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1863
5 UIKit 0x00c9719b -[UITableView layoutSubviews] + 241
6 UIKit 0x00c3392d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 279
7 libobjc.A.dylib 0x01bbc6b0 -[NSObject performSelector:withObject:] + 70
8 QuartzCore 0x01ac8fc0 -[CALayer layoutSublayers] + 240
9 QuartzCore 0x01abd33c _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 468
10 QuartzCore 0x01abd150 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
11 QuartzCore 0x01a3b0bc _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 324
12 QuartzCore 0x01a3c227 _ZN2CA11Transaction6commitEv + 395
13 QuartzCore 0x01adeb50 +[CATransaction flush] + 52
14 UIKit 0x00be19ff -[UIApplication _reportAppLaunchFinished] + 39
15 UIKit 0x00be24e1 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 2139
16 UIKit 0x00bf3315 -[UIApplication handleEvent:withNewEvent:] + 1022
17 UIKit 0x00bf424b -[UIApplication sendEvent:] + 85
18 UIKit 0x00be5cf8 _UIApplicationHandleEvent + 9874
19 GraphicsServices 0x02bd0df9 _PurpleEventCallback + 339
20 GraphicsServices 0x02bd0ad0 PurpleEventCallback + 46
21 CoreFoundation 0x01cf8bf5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
22 CoreFoundation 0x01cf8962 __CFRunLoopDoSource1 + 146
23 CoreFoundation 0x01d29bb6 __CFRunLoopRun + 2118
24 CoreFoundation 0x01d28f44 CFRunLoopRunSpecific + 276
25 CoreFoundation 0x01d28e1b CFRunLoopRunInMode + 123
26 UIKit 0x00be17da -[UIApplication _run] + 774
27 UIKit 0x00be365c UIApplicationMain + 1211
28 MyApp 0x0000257d main + 125
29 MyApp 0x000024b5 start + 53
)
这是我的代码,我在分配给核心数据之前检查 NSDate 对象是否为空。但仍然弹出相同的错误。
StudentDBManager *studentDBManager = [[studentDBManager alloc] init];
NSManagedObjectContext *context = [studentDBManager getManagedObjectContext];
Student *student = (Student*)[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];
student.firstName = firstName;
student.LastName = lastName;
if(dateOfBirth != NULL)
{
NSLog(@"NOT NULLLLLLLLLLLLLLLLLLLlllllLllll");
NSLog(@"DOB IS :%@", dateOfBirth);
student.dOB = dateOfBirth;
}
else
{
NSLog(@"dateOfBirth IS NULLLLLLLLLLLL");
}
student.createdDate = [NSDate date];
student.title = @"Student";