Why is a documented NSDateFormatter init method not getting recognized?
-
19-09-2019 - |
Question
I'm getting this warning (below) when trying to use NSDateFormatter. I am importing in my .h file so what is the prob? (Oh, and the same method generates an error (unrecognized selector sent...))
Here is the WARNING:
warning: no '-initWithDateFormat:allowNaturalLanguage:' method found
Here is the Code:
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] initWithDateFormat:@"EEE, dd, MMM yyyy HH:mm:ss ZZZ" allowNaturalLanguage:NO];
NSDate *date = [myFormatter dateFromString:@"Mon, 01 Feb 2010 20:25:37 +0000"];
NSLog(@"Time interval %d",[date timeIntervalSinceNow]);
Solution
Like Georg says, initWithDateFormat:allowNaturalLanguage is allowed only on Mac OS X and not on iPhone.
You can use NSDateFormatter on iPhone like this:
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
formatter.dateFormat = @"yyyy-d-M";
Further reading:
OTHER TIPS
The iPhone SDK hasn't got this init method, only the Mac OS X SDK has it. You have to use plain [[.. alloc] init] and set the properties yourself.
Sorry, I think it should be...
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-d-M"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow