如何读取保存为NSString的Core Data中的数据?

时间:2013-06-11 03:35:26

标签: ios core-data nsdate

我有CoreData实体,其中包含 dateUpdated 属性(类型NSString,格式为“2013-06-27”)。

当我想要读取此值时,它会返回 nil

Recipes *oldRecipe;
oldRecipe = [self getRecipeWithCode:i];
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"yyyy-MM-dd"];

NSString *oDate = oldRecipe.dateUpdated; 
//after this oDate is nil
NSDate *oldDate = [dtFormatter dateFromString:oDate];

我已经使用SQL Manager检查了实体中的值存在。

我怎样才能正确地做到这一点?

1 个答案:

答案 0 :(得分:0)

正如讨论中所述,oldRecipe对象是在 使用临时托管对象上下文的getRecipeWithCode方法。

托管对象只能存在于其创建的上下文中。如果上下文已取消分配,则oldRecipe将不会为nil,但访问其属性将始终返回nil。