views:

233

answers:

1

I'm trying to use the NSDate dateFromString method but I'm getting an warning and it's crashing the app. The code looks like:

NSString *pickerDate = [NSString stringWithFormat:@"%@", timeSelector.date];
NSDate *defaultDate = [NSDate dateFromString:pickerDate];

The warning is:

'NSDate' may not respond to '+dateFromString'.

It appears that method is deprecated (in the midst of an upgrade from XCode 2 to 3.

What alternate method can I use to create a date from a string?

+4  A: 

NSDateFormatter is the intended way for you to get an NSDate from an NSString.

The most basic usage is something like this:

NSString *dateString = @"3-Aug-10";

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"d-MMM-yy";

NSDate *date = [dateFormatter dateFromString:dateString];
Nick Forge