Why doesn't SwiftUI TextField currency format seem to work for Euros (EUR)?

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 }
}

Replies

Same here. Also tried "Barbados Dollars" (BBD), same result as with Euros. Only if I empty the whole field, including the currency sign, data entry is possible (and gets formatted correctly with the right currency).

USD works fine.

Any further insights on this?

  • No further insights, unfortunately, though it's been a while since I've looked closely at this issue and in the meantime I have just decided regrettably not to support the currencies that don't format correctly. Given how close we are to WWDC my plan is to wait to see whether the issue still exists in the new releases of the operating systems (hopefully they've been fixed) and, if the issues do still exist in the betas, to file a feedback.

Add a Comment