NSDate dateFromString not recommended?

I try to use the NSDate dateFromString method, but I get a warning and the application crashes. The code looks like this:

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

Warning:

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

It seems that the method is deprecated (at the height of the upgrade from Xcode 2 to 3.

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

+3
source share
3 answers

NSDateFormatterdesigned for you to get NSDatefrom NSString.

The most basic use 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];
+13
source

. dateFormat @ "YYYY/M/d H: m: s" @ "YYYY/MM/dd HH: mm: ss" . iPhone 4. (Xcode 4.5 iOS 6.)


NSDateFormatter *dateFormatter1;
NSString *dateStr1=@"";
NSDate *gantanDate1;

dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setLocale:[NSLocale systemLocale]]; 
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]]; 
dateFormatter1.dateFormat=@"YYYY/MM/dd HH:mm:ss";
dateStr1=@"2012/1/1 00:00:00"];
//  in his case,   dateStr1=pickerDate;  
gantanDate1 = [dateFormatter1 dateFromString:dateStr1];
+2

Hi, Silber, everyone says the same thing to convert a date to a date. Try this, I think you used two date formats in two places where you store the date in a string and get the date from string.right. Try using the same date formats in both places. This will solve your problem.

0
source

Source: https://habr.com/ru/post/1757938/


All Articles