开发者

Save NSDate to CoreData

i want to save NSDate from XML to coreData. I dont know how to set the Value for NSDate. Can some开发者_如何学运维one give me little help? My Entity attribute is NSDate. Or should it be a String and Save the Date as String??

[Sets setValue:[TBXML textForElement:xmlDate] forKey:@"beginDate"];

Thanks for any response and help,

brush51


Whether you are intentionally obfuscating or not, you don't say it, so I'm going to guess that:

  • [TBXML textForElement:xmlDate] returns an NSString
  • beginDate is a Date property of entity Sets

In which case your code is obviously incorrect: you must pass an instance of NSDate to setValue:forKey:. Your code would look like:

NSDateFormatter *myXMLdateReader = [[NSDateFormatter alloc] init];
[myXMLdateReader setDateFormat:@"yyyy-MM-dd"]; // for example
NSDate *itsDate = [myXMLdateReader dateFromString:[TBXML textForElement:xmlDate]];
[myXMLdateReader release];
[Sets setValue:itsDate forKey:@"beginDate"];


It is no problem to add NSDate to CoreData. All you need to do is to convert NSString from XML file to NSDate object. For converting NSString <-> NSDate you can use NSDateFormatter class. In example below you can see how I do this:

NSDateFormatter *parseFormatter = [[[NSDateFormatter alloc] init] autorelease];
[parseFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]];
[parseFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSString *dateString = @"Mon, 02 May 2011 21:12:56 +0000";
NSDate *dateToAdd = [parser.parseFormatter dateFromString:dateString];

You can set needful date format and use this code in your project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜