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

TextField Double Empty Value

Forums > SwiftUI

I'd like to provide a textfield to accept a dollar amount with formatting such as 00.00. The problem I am having is that when the user selects the field it has a default value of 0 and the user has to delete the 0 before inputting the correct amount. I looked at a few solutions but I'm not sure what I'm doing wrong because nothing I try is working.

@State var listItems = [Item]()
    @State var addItemName: String = ""
    @State var addItemValue: Double?
    @State var addItem: Bool = false

    var body: some View {
        ZStack {
                VStack {
                    NavigationView {
                        List {
                            ForEach(listItems, id: \.self) { item in
                                let formattedFloat = String(format: "%.2f", item.value)
                                Text("\(item.name) \(formattedFloat)")
                            }
                        }

SOLUTION I was able to fix this on my own but I'm sure their are better ways to do this. Here is what I have.

@State var listItems = [Item]()
    @State var addItemName: String = ""
    @State var addItemValue: String = ""
    @State var addItem: Bool = false

    var body: some View {
        ZStack {
                VStack {
                    NavigationView {
                        List {
                            ForEach(listItems, id: \.self) { item in
                                let valueFloat = (item.value as NSString).doubleValue
                                let formattedFloat = String(format: "%.2f", valueFloat)
                                Text("\(item.name) \(formattedFloat)")
                            }
                        }

3      

You do not show the definition of your Item object. But you're right, you could improve your solution.

For example:

ForEach(listItems, id: \.self) { item in
    let valueFloat = (item.value as NSString).doubleValue   // Stop! Refactor and move to Item struct
    let formattedFloat = String(format: "%.2f", valueFloat) // Stop! Refactor and move to Item struct
    Text("\(item.name) \(formattedFloat)")
}

You are creating Text views for each Item object in your listItems array.

Consider moving ALL this code into your Item struct. That is, consider adding computed vars to your Item struct.

struct Item {
    // Previous declarations
    //==== computed var ======
    var itemVarAsFloat : Float {
        (self.value as String).doubleValue  // Seems awkward
    }
    var formattedVar: String {
        String(format: %.2f", itemVarAsFloat)
    }
}

Then your view is greatly simplified! In your view, try to DECLARE what you want to see. Let other structs do the computing.

ForEach(listItems, id: \.self) { item in
    Text("\(item.name) \(item.formattedVar)")  // DECLARE what you want to see.
}

3      

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!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.