SwiftUI's TextField's format parameter accepts a ParseableFormatStyle of .currency. This is a super useful improvement for what I imagine is a really common use case. But while it mostly works seamlessly for me, for some reason it doesn't seem to work if the currency is set to Euros (EUR).
If the currency is set to EUR as in my example below, no matter what you enter in the TextField, the value resolves to 0 formatted as a localised representation of EUR 0.00. It's as if the ParseableFormatStyle doesn't recognise any input as being a valid format of EUR. If you change the currency from EUR to any of the other currencies I've tried it works fine.
Note: There is another issue I've encountered with the .currency format of TextField which I've written about in a separate post which you can find here.
import SwiftUI
struct ContentView: View {
@State private var amount = Decimal()
@State private var currency: Currency = .EUR // change this to any other currency to get different (and expected) results
var body: some View {
CurrencyAmount(
title: "Some label",
amount: $amount,
currency: $currency)
}
}
struct CurrencyAmount: View {
let title: String
@Binding var amount: Decimal
@Binding var currency: Currency
let prompt: String = ""
var body: some View {
TextField(
title,
value: $amount,
format: .currency(code: currency.rawValue),
prompt: Text(prompt))
}
}
}
enum Currency: String, CaseIterable, Identifiable {
case AUD, CAD, EUR, GBP, NZD, USD
var id: String { self.rawValue }
}