我被困在愚蠢的事情上。这个应用程序是OSX核心数据应用程序。我正在调用一个动作表来播放日期。关闭工作表后,屏幕上会正确显示所选日期。但是,我无法想出如何保存日期。
任何帮助表示赞赏。这是我在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];
}
}
}
答案 0 :(得分:0)
我发现这里的答案非常有用,但这post让我思考。所以今天,代码看起来如下,它的效果很棒!
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"];
}