LocalizedStringKey.formatted(locale:tableName:bundle:comment:) crash
vladyslavsosiuk opened this issue · 3 comments
Description
LocalizedStringKey.formatted(locale:tableName:bundle:comment:) crashes with :
Could not cast value of type 'SwiftUI.LocalizedStringKey.FormatArgument.Token' (0x1e8d39410) to 'NSFormatter' (0x1169d9e08).
on the line 608
Checklist
- I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
- If possible, I've reproduced the issue using the
main
branch of this package. - This issue hasn't been addressed in an existing GitHub issue or discussion.
Expected behavior
There is no crash
Actual behavior
The library crashes:
Steps to reproduce
Run this code:
struct ContentView: View {
var body: some View {
ForEach(
[
TextState(
// "Hello world"
"Hello \(Text("world"))" // Text interpolation into the LocalizedStringKey crashes
)
],
id: \.self
) {
Text(
$0
)
}
}
}
SwiftUI Navigation version information
Destination operating system
iOS 18.0
Xcode version information
Xcode 15.4
Swift Compiler version information
swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0
Hi @vladyslavsosiuk, ideally this would not crash, and if there's an easy fix we will happily accept a PR. However, we do not think it is ever correct to interpolate Text
into a TextState
. The whole reason for TextState
to exist is to be Equatable
and allow for testing. The Text
type, while Equatable
, does not have a straightforward conformance and is completely untestable.
And so if you do not need testability from your domain, you can just use Text
entirely and forget about TextState
. And if you do need testability, then you can use TextState
and should not use Text
at all.
Hi @mbrandonw, thank you for the detailed reply. That makes sense. I just wanted to raise awareness about the crash
Going to close this for now since it's not how the library is intended to be used, but if you can share a crash from real world use we can definitely take a closer look!