Rob Rob - 2 months ago 10
Objective-C Question

How to init a NSDate with a NSString?


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

Answer

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