BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: WeSplit App (Project 1): how to clear the checkAmount on tap?

Forums > 100 Days of SwiftUI

I'm currently trying to slightly improve the UX of the WeSplit app so that when you tap on the checkAmount text field, you don't have to delete the "0.00" default value before entering your own value. In other words, the text field is blank, and you can just start inputting your bill amount.

I'm finding it quite difficult to do!

What's the best way of approaching this?

   

@Bnerd  

I declared the checkAmount as String and set it to ""

@State private var checkAmount = ""

In the view is used as below

TextField("Amount", text: $checkAmount)
                        .keyboardType(.decimalPad)

Finally in the calculations I just wrap it around Double i.e. Double(checkAmount)

1      

@geoff  

You could also declare as an optional Double and then nil coalesce down to 0.

@State private var checkAmount: Double?
let tipValue = (checkAmount ?? 0) / 100 * tipSelection
let grandTotal = (checkAmount ?? 0) + tipValue

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

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.