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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.