0

Possible Duplicate:
Converting NSString to NSDate (and back again)

I'm still beginner at obj-C/iOS development... I've created a class containing a NSDate *date, and I get the data from a XML file. So I want to do something like this :

[myItem setDate:[NSDate dateWithString:xmlDate]];

I get the error :

No known class method for selector 'dateWithString:'

What is the best way to do it ? Thanks

Community
  • 1
  • 1
Rob
  • 15,732
  • 22
  • 69
  • 107

3 Answers3

4

You need an NSDateFormatter. See this previous question or this previous question or this previous question or just the Apple Date Formatter guide for more details.

And please remember to search for duplicate questions before posting.

Community
  • 1
  • 1
Zach Lipton
  • 1,850
  • 1
  • 14
  • 28
2

I would recommend you to use a NSDateFormatter instance for this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// set the appropriate format in this string
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:xmlDate];
[dateFormatter release];
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
1

To make an NSDate object from a NSString you should use NSDateFormatter. More info can be found on Apple's website:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

Hope it helps

Novarg
  • 7,390
  • 3
  • 38
  • 74