Ios 将NSDate存储在CoreData中

Ios 将NSDate存储在CoreData中,ios,objective-c,core-data,nsdate,nsdateformatter,Ios,Objective C,Core Data,Nsdate,Nsdateformatter,我从jSON返回中得到的字符串格式如下:2014-06-13T11:11:16.2 我用来存储的代码是: NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM-ddTHH:mm:ss.S"]; [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; NSString

我从jSON返回中得到的字符串格式如下:2014-06-13T11:11:16.2

我用来存储的代码是:

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-ddTHH:mm:ss.S"];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
NSString *dateStr;
NSDate *formattedDate;
dateStr = NSLocalizedString([inventoryItem objectForKey:@"PurchaseDate"], nil);
formattedDate = [formatter dateFromString:dateStr];
newItem.purchaseDate = formattedDate;
我哪里出错了?

将格式更改为:

@"yyyy-MM-dd'T'HH:mm:ss.S"

(您需要在格式中围绕静态字母数字字符引用),您也可能想考虑是否使用<代码> EngUs或<代码> EnUsUSPOSIX (是来自用户或来自Web某处的日期).< /P> < P>格式日期中的NIL意味着NSDateFormatter无法解析字符串;模板有问题

在这段摘自苹果公司《日期格式化程序指南》的摘录中,他们似乎在模板字符串中的几个项目周围加了引号

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
你必须修改这个来解释你的10分之一秒,以下是我的最佳猜测:

[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'S"];

为什么使用NSLocalizedString

请尝试使用此代码->

dateStr = [inventoryItem objectForKey:@"PurchaseDate"];//your formated string
是否为“newItem”创建新实体

最后,您是否保存了更改

NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } 
}

祝你好运

这个代码会给你什么结果?格式化日期为零吗?如果您记录dateStr,它是否按照您在第一句话中显示的格式?dateStr-2014-06-05T13:43:45.03 formattedDate-(null)您好。我们希望你不要在你的帖子上签名,添加问候,或者添加感谢和未来的感激。如果可以的话,让你的问题简洁明了——不管怎样,最好是这样编辑的。
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } 
}