TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 37 - iExpense Amount bug

Forums > 100 Days of SwiftUI

Hi, just completed Day 37 and followed the tutorials for the iExpense app, but in the end i had a problem while trying to give an input for the "amount" Double with the currency format set to "EUR".

When i tried to type a value and hit return or the button "Save", the value would automatically reset to the default one (in this case "€0.00" instead of returning the one i wrote.

My code was this:

TextField("Amount", value: $amount, format: .currency(code: "EUR"))
                    .keyboardType(.decimalPad)

While searching for a solution online i ended up fixing the problem with this code:

TextField("Amount", value: $amount, format: .currency(code: Locale.current.currency?.identifier ?? "EUR"))
                    .keyboardType(.decimalPad)

But i wanted to understand why the first one didn't work, what makes the difference in the second one that fixed the problem?

2      

The only difference I see between the two lines you posted is that the formatting of the currency is fetched from the user's preferences, defaulting to EUR if not available. It shouldn't have any impact on the flow of data to the bound $amount variable, or the behavior of buttons.

I'm inclined to think that the bug was fixed somewhere else. Or, were you using the preview rather than building with CMD+R? I've noticed that preview can have some weird quirks.

2      

Using curreny code be displayed properly is a common issue , you can use number formatter, even your first code is correct i do not remember the exact solution i had for iExepnese but i share what i do commonly to get the amount in the currency i want , now you can use Locale.current.currency?.identifier ?? "EUR" aslo in place of just "EUR" for dynamic result of local currency, so if some one is in japen it will show yen, on simulator is shows $ though as default local

struct ContentView: View {
    @State var amount: Decimal = 0.0
    var body: some View {
        VStack {
            TextField("Amount", value: $amount, format: .currency(code:  "EUR"))
                .keyboardType(.decimalPad)
            Button("save") {
                let formatter = NumberFormatter()
                formatter.numberStyle = .currency
                formatter.currencyCode = "EUR"
                if let formattedAmount = formatter.string(from: NSDecimalNumber(decimal: amount)) {
                    print(formattedAmount)
                }
            }
        }
        .padding()
    }
}

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.