Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Last active February 2, 2019 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonjs/988bd5b7ca344f43a59a36b26cf3b9c8 to your computer and use it in GitHub Desktop.
Save samsonjs/988bd5b7ca344f43a59a36b26cf3b9c8 to your computer and use it in GitHub Desktop.
Crashing bug with DateComponentsFormatter on iOS 12
When called during launch with nil calendar and abbreviated units, DateComponentsFormatter.string(from:)
crashes attempting to call -[__NSCFString locale].
Area:
Foundation
Summary:
Due to rdar://problem/44835278 we're using DateComponentsFormatter with its calendar property set to nil
to force it to use the en_US_POSIX locale, since we can't set the locale directly. The unitsStyle
property is .abbreviated. This generally works, however it crashes consistently when calling
string(from: TimeInterval) during launch, unrecognized selector -[__NSCFString locale].
Steps to Reproduce:
An example project is included for you to run and observe the crash. You can execute the following code in
any app's didFinishLaunching app delegate method:
let duration: TimeInterval = 30_000
let formatter = DateComponentsFormatter()
formatter.calendar = nil
formatter.unitsStyle = .abbreviated
let abbreviated = formatter.string(from: duration)!
print(abbreviated)
Expected Results:
No crash. Returns a string.
Actual Results:
Crash.
Version/Build:
iOS 12.1.3 (16D39)
Configuration:
iPhone 7 Plus and the iPhone 8 simulator
----
I just tried something slightly different in my code (not initializing the formatter in a static let property)
and this time it crashed sending the locale selector to BSActionListenerToken instead of __NSCFString, so
there might be a race or something more interesting with memory going on here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment