Reputation: 1304
I have a simple application: a user selects a date in a UIDatePicker and fills in some fields and through saving through Core Data, the Table View controller is supposed to display just a list of years that have been selected in the date picker. If someone chose 1 date with 2013 as the year and then another one, there should only be 1 entry for 2013 in the table view.
I have the back-end logic working for the duplication checks. My issue is with the actual predicate on this table view controller in the FetchedResultsController. It's causing my date to be nil and Xcode is giving me a humorous message too!
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.
The code doing the predicates is:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit ) fromDate:self.date.dateOfEvent];
//create a date with these components
NSDate *startDate = [calendar dateFromComponents:components];
[components setYear:components.year]; //reset the other components
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"dateOfEvent == %@",startDate];
NSLog(@"New Date = %@", startDate);
I have a property for the Date Entity called date and the dateOfEvent attribute is the NSDate that I'm trying to extract just the year out off!
The NSLog gives me 2001-01-01 00:00:00 +0000
so it's clearly not getting that date.
If anyone is able to assist with this into a) why I'm getting a nil date effectively and b) thoughts on Xcode's error and c) how would I update cellForRow to display just the year?
Thanks!
Upvotes: 0
Views: 1194
Reputation: 80273
First, self.date.dateOfEvent
is most likely zero. That is what the sarcastic error message is indicating. However, your log message seems to contain a valid year, so maybe stepping through the code with he debugger would help.
Second,
[components setYear:components.year]
is the same as
components.year = components.year
which does absolutely nothing. A date from components that only consist of year will have no months, days, hours, etc. (they will all be zero).
Third, the predicate will only return a value if the date in the database is exactly the same (down to the second) which is likely not the case. Assuming you have a certain year you want to check, you need to construct a date interval to find the relevant records.
NSInteger year = 2011;
NSDateComponents *components = [[NSDateComponents alloc] init];
components.year = year;
NSDate *startOfYear = [[NSCalendar currentCalendar] dateFromComponents:components];
components.year = 1; // (one year)
NSDate *endOfYear = [[NSCalendar currentCalendar] dateByAddingComponents:components
toDate:startOfYear options:0];
then use a predicate like this:
[NSPredicate predicateWithFormat:
@"dateOfEvent > %@ && dateOfEvent <= %@", startOfYear, endOfYear];
Upvotes: 1