简体   繁体   English

如何从保存为NSString的CoreData中读取日期?

[英]How to read date from CoreData saved as NSString?

I have CoreData entity which contain an attribute dateUpdated (type NSString with format "2013-06-27"). 我有CoreData实体,其中包含一个属性dateUpdated (类型为NSString ,格式为“ 2013-06-27”)。

When I want to read this value it returns nil . 当我想读取此值时,它返回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];

I have already checked value presence in entity with SQL Manager. 我已经使用SQL Manager检查了实体中的值是否存在。

How can I correctly do it? 我该怎么做呢?

As it turned out in the discussion, the oldRecipe object is created in the getRecipeWithCode method using a temporary managed object context. 正如在讨论中所oldRecipegetRecipeWithCode使用临时托管对象上下文在getRecipeWithCode方法中创建了getRecipeWithCode对象。

A managed object can only live in the context that it was created in. If the context is deallocated then oldRecipe will not be nil, but accessing its properties will always return nil. 受管对象只能存在于创建该对象的上下文中。如果已释放该上下文,则oldRecipe不会为nil,但是访问其属性将始终返回nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM