ios - stringFromDate and dateFromString sometimes return null -
I just have to format the format that comes in this format "2013: 07: 24 11:05:04" In this format, the "24-lug-2013", this is the code I do to:
NSDateFormatter * dtf = [[NSDateFormatter alloc] init]; [Dtf setet format: @ "yyyy: MM: dd hh: mm: ss"]; NSDate * d = [dtf dateFromString: self.creationDate]; NSDateFormatter * dateFormatStr = [[NSDateFormatter alloc] init]; [DateFormatStr setDateFormat: @ "d-MMM-yyyy"]; NSString * strDate = [dateFormatStr stringFromDate: d]; NSLog (@ "% @", strDate);
This code works right, the problem is that after making some calls in this method, sometimes I get an empty string, but the format of the input is still the same.
For example, with this string as an input "2013: 07: 24 11:05:04" I get this: 24-look -13, which in fact I want, but this case After 7 calls, input with "2013: 07: 24 13:47:31" I get null NSDT and as a result the result is empty in the string.Where's the problem ?? Thanks
Date formats return to zero, when the date string is passed in it, then the format Does not match. The code you posted uses dateFromString on the input string. If he fails, your NSDT "D" will become zero, and hence the call to string will likely be nil. (Although string format can also crash if you pass in NSDT of zero)
Comments
Post a Comment