UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

@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)

3      

@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

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.