Reputation: 5101
In my app I have a UITableView with
sections. Each entity object is sorted based on a transient attribute called 'sectionIdentifier', which is defined in a NSManagedObject
subclass called ToDoItem.m
When the app is launched, all objects are correctly sorted and shown under the expected section.
On the table view controller there is and add button to add a new object, just after taping on it, the app throws following warning:
2014-01-15 11:19:19.459 To-Do Pro Light[21914:a0b] date= (null)
2014-01-15 11:19:19.461 To-Do Pro Light[21914:a0b] todayDate = 2014-01-15 18:19:19 +0000
2014-01-15 11:19:19.473 To-Do Pro Light[21914:a0b] *** -[__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 0x01ab8475 -[__NSCFCalendar components:fromDate:] + 101
1 CoreFoundation 0x01b5496c -[_NSCopyOnWriteCalendarWrapper components:fromDate:] + 92
2 To-Do Pro Light 0x00008e95 -[ToDoItem sectionIdentifier] + 357
3 Foundation 0x0145b947 _NSGetUsingKeyValueGetter + 119
4 CoreData 0x0029d75a _PF_Handler_Public_GetProperty + 122
5 CoreData 0x0029d685 -[NSManagedObject valueForKey:] + 149
6 Foundation 0x0147aa5a -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 409
7 CoreData 0x003696ed -[NSFetchedResultsController(PrivateMethods) _objectInResults:] + 77
8 CoreData 0x00369995 -[NSFetchedResultsController(PrivateMethods) _preprocessInsertedObjects:insertsInfo:newSectionNames:] + 389
9 CoreData 0x0036c1bd -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 1197
10 Foundation 0x01500bf9 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke + 40
11 CoreFoundation 0x01b06524 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
12 CoreFoundation 0x01a5e00b _CFXNotificationPost + 2859
13 Foundation 0x0143a951 -[NSNotificationCenter postNotificationName:object:userInfo:] + 98
14 CoreData 0x00271173 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 83
15 CoreData 0x0031078f -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 367
16 CoreData 0x0026c608 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2152
17 CoreData 0x0026bd99 -[NSManagedObjectContext processPendingChanges] + 41
18 CoreData 0x0023ffe1 _performRunLoopAction + 321
19 CoreFoundation 0x01a724ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
20 CoreFoundation 0x01a7241f __CFRunLoopDoObservers + 399
21 CoreFoundation 0x01a50344 __CFRunLoopRun + 1076
22 CoreFoundation 0x01a4fac3 CFRunLoopRunSpecific + 467
23 CoreFoundation 0x01a4f8db CFRunLoopRunInMode + 123
24 GraphicsServices 0x038bb9e2 GSEventRunModal + 192
25 GraphicsServices 0x038bb809 GSEventRun + 104
26 UIKit 0x0059bd3b UIApplicationMain + 1225
27 To-Do Pro Light 0x000078ad main + 141
28 libdyld.dylib 0x020e6725 start + 0
)
2014-01-15 11:19:19.478 To-Do Pro Light[21914:a0b] Tmp= 0
2014-01-15 11:19:19.524 To-Do Pro Light[21914:a0b] Fecha del todo = 2014-01-15 18:19:19 +0000
I can't understand it, but in the only file there are calendar components is in the ToDoItem.m NSManagedObject
class, which I show you:
#import "ToDoItem.h"
#import "ToDoGroup.h"
#import "ToDoSubItem.h"
@implementation ToDoItem
@dynamic todoDescription;
@dynamic todoName;
@dynamic todoDueDate;
@dynamic sectionIdentifier;
@dynamic todogroup;
@dynamic todosubitems;
-(NSString *)sectionIdentifier{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp){
NSDate *date = self.todoDueDate;
NSDate *todayDate = [NSDate date];
NSLog(@"date= %@",date);
NSLog(@"todayDate = %@",todayDate);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDateComponents *date1Components = [calendar components:comps fromDate:date];
NSDateComponents *date2Components = [calendar components:comps fromDate:todayDate];
date = [calendar dateFromComponents:date1Components];
todayDate = [calendar dateFromComponents:date2Components];
if([date
compare:todayDate] == NSOrderedSame) {
tmp = @"1";//TODAY
}
else if([date
compare:todayDate] == NSOrderedDescending){
tmp = @"2";//OVERDUE
}
else if ([date
compare:todayDate] == NSOrderedAscending){
tmp =@"0";//UPCOMING
}
NSLog(@"Tmp= %@",tmp);
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
return tmp;
}
@end
Any help is welcome and if you want me to show you my tableView controller, no problem..... Thank you in advance.
Upvotes: 0
Views: 198
Reputation: 539745
That means that
NSDate *date = self.todoDueDate;
returned nil
, i.e. you did not set a todoDueDate
for the newly created object.
You cannot compute date components from a nil
date.
Upvotes: 1