简体   繁体   English

将NSDate存储在CoreData中

[英]Store NSDate in CoreData

The string that I'm getting from the jSON return is formatted as follows: 2014-06-13T11:11:16.2 我从jSON返回获取的字符串的格式如下:2014-06-13T11:11:16.2

The code I'm using to store is: 我用来存储的代码是:

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;

Where am I going wrong? 我要去哪里错了?

Change the format to: 将格式更改为:

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

(you need quotes around static alpha-numeric characters in the format) Also, you might want to consider whether you want to use en_US or en_US_POSIX (is the date coming from a user or from the web somewhere). (您需要在格式中的静态字母数字字符周围加上双引号)此外,您可能要考虑要使用en_US还是en_US_POSIX (日期是来自用户还是来自网络的某个日期)。

The nil in formattedDate means that the NSDateFormatter failed to parse the string; formattedDate中的nil表示NSDateFormatter解析字符串失败; something is wrong with the template. 模板出了点问题。

In this excerpt from Apple's Date Formatter guide, they seem to put quotes around several of the items in the template string. 在Apple的Date Formatter指南的摘录中,他们似乎在模板字符串中的几个项目周围加上了引号。

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];

You will have to modify this to account for your 10ths of a second, here's my best guess: 您将不得不对此进行修改,以使其占十分之一秒,这是我的最佳猜测:

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

Why you use NSLocalizedString? 为什么使用NSLocalizedString?

Try use this code-> 尝试使用此代码->

dateStr = [inventoryItem objectForKey:@"PurchaseDate"];//your formated string

Are you create new entity for "newItem"? 您是否要为“ newItem”创建新实体?

newItem = [NSEntityDescription insertNewObjectForEntityForName:newItemEntityName inManagedObjectContext:managedObjectContext];

And in the end are you save you changes? 最后,您是否保存了更改?

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();
    } 
}

Good luck! 祝好运!

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

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