views:

224

answers:

1

I am trying to do some basic operations with Dates on the iPhone. I want to take a string like "2010-02-19" and turn it into a string like "Friday, Feb 19." The strings I will be receiving will always be in 2010-02-19. Right now, I am running the following code that works, but it produces a warning that says "NSDate may not respond to +dateWithNaturalLanguageString:"

NSDate *date = [NSDate dateWithNaturalLanguageString:scheduled];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"EEEE, MMM dd"];

return [df stringFromDate:date];

The code works, but it produces that warning. Should I worry about it, or is this OK on the iPhone. Is there another NSDate initializer that I should be using?

+1  A: 

+dateWithNaturalLanguageString seems to be only available on the Mac : NSDate Class Reference in Mac Dev Center vs. NSDate Class Reference in iPhone Dev Center

Also, the documentation says that:

It may give unexpected results, and its use is strongly discouraged

In the overview section of the class reference you can find the following information:

NSDate provides several methods to interpret and to create string representations of dates (for example, dateWithNaturalLanguageString:locale: and descriptionWithLocale:). In general, on Mac OS X v10.4 and later you should use an instance of NSDateFormatter to parse and generate strings using the methods dateFromString: and stringFromDate:—see NSDateFormatter on Mac OS X 10.4 for more details.

Given that this is only officially available on the Mac, but not on the iPhone, I wouldn't use it in an iPhone project. Even on the Mac it seems that Apple recommends using an NSDateFormatter instead.

Thomas Müller