【问题标题】:Dates aren't saving to Core Data Object日期未保存到核心数据对象
【发布时间】:2014-12-02 16:48:56
【问题描述】:

我正在从 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;

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

【问题讨论】:

    标签: ios objective-c core-data nsdate


    【解决方案1】:

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

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

    顺便说一句:你不应该复数你的实体名称,因为这不合逻辑而且很混乱。
    请改用Subject

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

    【讨论】:

    • 如何查看?我在另一个控制器中添加了一个时间表,然后我试图在这里为一个主题设置这个时间。所以一开始这个时候没有附上Subjects实体,我想附上这里。
    • 如果TimeOfSchedule 对象已经存在,将它传递给这个控制器或方法。如果没有,请创建它。您添加与schedule.subject = subject;的关系
    • 明白。我试图传递这个表的一个属性,而不是传递这个对象。谢谢。
    猜你喜欢
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2020-10-09
    • 1970-01-01
    相关资源
    最近更新 更多