SwiftUI date formatter uses wrong locale in navigationTitle?

(I've posted more details of my question on Stack Overflow: https://stackoverflow.com/q/64566387/23649)

This simple SwifUI code produces a strange result: the content view shows "October 27, 2020", while the navigation title shows "2020 M10 27".

I'm using Xcode 12.1 and the iOS 14.1 simulator. The same happens on my device running 14.1.

Code Block swift
struct ContentView: View {
var body: some View {
NavigationView {
Text(Date(), style: .date)
.navigationTitle(
Text(Date(), style: .date)
)
}
}
}


I tried this, but got the same result:
Code Block swift
let formatter = DateFormatter()
formatter.dateStyle = .long
Text(Date(), formatter: formatter)


Finally, pre-formatting the text seems to give the expected results:
Code Block swift
Text(formatter.string(from: Date()))


I tried to debug by subclassing DateFormatter and overriding string(for obj: Any?). I found that for the title bar, the formatter's locale is set to " (fixed)", i.e. an empty string as the locale identifier. When the content view is being displayed, the formatter's locale is set to en_US as I'd expect in my system locale. I tried wrapping my views with .environment(\.locale, Locale.current), but that didn't help.

Is this just a bug? Do I really need to pre-format my dates as a workaround?
Answered by DTS Engineer in 643132022
I tried this here in my office (using Xcode 12.1 targeting iOS 12.1, so latest GM stuff basically) and saw the same results. And, yeah, that’s super weird. You should definitely file a bug about it.

Please post your bug number, just for the record.

Fortunately your workaround, while annoying, is pretty straightforward.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer
I tried this here in my office (using Xcode 12.1 targeting iOS 12.1, so latest GM stuff basically) and saw the same results. And, yeah, that’s super weird. You should definitely file a bug about it.

Please post your bug number, just for the record.

Fortunately your workaround, while annoying, is pretty straightforward.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Filed FB8831937.
SwiftUI date formatter uses wrong locale in navigationTitle?
 
 
Q