I am stuck on something dumb. This app is an OSX core-data app. I'm calling an action sheet to play with dates. The date selected is correctly shown on the screen after the sheet is closed. However, I cannot figure how to save the date.
Any help appreciated. This is the code I'm working with in my AppDelegate.m:
NSDate *theDate = [self.purchaseDatePicker dateValue];
if (theDate)
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *formattedDateString;
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
formattedDateString = [formatter stringFromDate:theDate];
[self.purchaseDate setStringValue: formattedDateString];
self.item.purchaseDate = theDate;
[self.item setPurchaseDate:theDate];
[self.item setPurchaseDate:self.item.purchaseDate];
NSLog(@"theDate is %@", theDate); <-- this is fine.
NSLog(@"item.purchaseDate is %@", self.item.purchaseDate); <-- this returns NULL
NSError *error = nil;
NSManagedObjectContext *moc = _coreDataController.mainThreadContext;
if (![moc save:&error])
{
[[NSApplication sharedApplication] presentError:error];
}
}
}
I found the answers here very helpful, but this post really got me thinking. So today, the code looks like below, and it works great!!
1.
[itemArrayController setValue: [DatePicker1 dateValue] forKeyPath: @"selection.date1"];
2.
if ([itemArrayController valueForKeyPath: @"selection.date1"] == NULL) {
date1 = [NSDate date];
[datePicker1 setDateValue: date1];
} else {
[datePicker1 setDateValue: [itemArrayController valueForKeyPath: @"selection.date1"]];
date1 = [itemArrayController valueForKeyPath: @"selection.date1"];
}