BLACK FRIDAY SALE: Save big 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 RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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.