gpt4 book ai didi

ios - 日期未保存到核心数据对象

转载 作者:行者123 更新时间:2023-11-28 21:54:00 25 4
gpt4 key购买 nike

我正在从 NSSting 转换 NSDate,然后以这种方式将这个日期添加到 Core Data 对象

- (IBAction)save:(id)sender
{

managedObjectContext = [AppDelegate sharedDelegate].managedObjectContext;

NSUInteger selectedString = [subjectPicker selectedRowInComponent:0];

Subjects *selectedSubject = [subjectForEventArray objectAtIndex:selectedString];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"Europe/Kyiv"];
[dateFormatter setDateFormat:@"hh:mm"];

NSLog(@" %@ %@ ", self.startDateForSubject, self.endDateForSubject);
NSDate *sDate = [dateFormatter dateFromString:self.startDateForSubject];
NSDate *eDate = [dateFormatter dateFromString:self.endDateForSubject];


NSLog(@"%@ %@", eDate, sDate);


selectedSubject.timeOfSubject.startDate = sDate;
selectedSubject.timeOfSubject.endDate = eDate;
selectedSubject.weekDay.dayOfWeek = _dayString;





NSError *error;

if ([managedObjectContext save:&error])
{
if (self.delegate) {
[self.delegate selectedTeacherToControllerForIndexPath:self.scheduleCellIndexPath];
} else {
NSLog(@"delegate is not set and can not execute code");
}
NSLog(@"=== %@ === %@ === %@ ===",selectedSubject.timeOfSubject.startDate, selectedSubject.timeOfSubject.endDate, selectedSubject.weekDay.dayOfWeek);
[self.navigationController popViewControllerAnimated:YES];
}


}

这是日志窗口

2014-12-02 18:35:00.115 StudentSchedule[1003:38141]  04:50 06:45 
2014-12-02 18:35:00.117 StudentSchedule[1003:38141] 2000-01-01 04:45:00 +0000 2000-01-01 02:50:00 +0000
2014-12-02 18:35:00.118 StudentSchedule[1003:38141] === (null) === (null) === Monday, 11.12 ===

NSManagedObject 子类 Subjects 与另一个表“TimeOfSchedule”有关系 timeOfSubject,并且我有属性 NSDate。

@class ReadySubject, Teacher, TimeOfSchedule;

@interface Subjects : NSManagedObject

@property (nonatomic, retain) NSString * article;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) Teacher *teacherOfSubject;
@property (nonatomic, retain) TimeOfSchedule *timeOfSubject;

@class Subjects;

@interface TimeOfSchedule : NSManagedObject

@property (nonatomic, retain) NSDate * endDate;
@property (nonatomic, retain) NSDate * startDate;

问题是,这个日期没有保存在我的核心数据中,有空值,我不明白为什么?

最佳答案

看起来 timeOfSubject 为零。确保您的托管对象已正确设置和填充。

如果您设置了 timeOfSubject 的属性,它不会自动插入一个新的 TimeOfSchedule 对象作为关系。您必须使用 insertNewObject... 创建它。并将其附加到 Subjects 实体。

顺便说一句:您不应该将您的实体名称复数化,因为这不合逻辑且相当困惑。
请改用 Subject

至于“正确设置”,您是否创建了从TimeOfSchedule 回到Subject 的反向关系?至少您在代码中将其遗漏了。

关于ios - 日期未保存到核心数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27254662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com