dateFromString results in nil

In a completely new project using Objective-C, when using "NSDateFormatter" under the conditions mentioned, setting initWithLocaleIdentifier to "NSCalendarIdentifierGregorian" results in "dateFromString" returning nil. This issue is occurring specifically in iOS 18 beta 1 and 2, and it's causing me significant trouble.

This process works correctly on iOS 17 and earlier versions.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setTimeStyle:NSDateFormatterFullStyle];
	[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSCalendarIdentifierGregorian]];
	[formatter setDateFormat:formatStr];
	NSDate *date = [formatter dateFromString:formatStr];

"date" is nil.

Can you show what formatStr is set to?

I made a mistake in the writing. With this, the date will become nil.

NSString *format = @"yyyyMMdd";

NSString *formatStr = @"20240619";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterFullStyle];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSCalendarIdentifierGregorian]];
[formatter setDateFormat:format];
NSDate *date = [formatter dateFromString:formatStr];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSCalendarIdentifierGregorian]];

This creates a locale with a calendar identifier. That makes no sense. If you’re using an NSDateFormatter with a fixed format, you must pin the locale to en_US_POSIX. See QA1480 NSDateFormatter and Internet Dates for the full backstory.

Also, you’re parsing a date without a time component. That’s trickier than you might think. See Parsing Dates Without Times.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I was able to resolve it using the method you suggested. Thank you very much. However, the issue of becoming nil in iOS 18 beta 4 no longer occurs.

Ultimately, is this issue specific to beta versions? Should we revert to our previous solution of setting NSCalendarIdentifierGregorian?

dateFromString results in nil
 
 
Q