Shpigford
Shpigford

Reputation: 25348

How do I get a Double with a default value to be blank/empty in a form field in SwiftUI?

I've got a form in SwiftUI where one of the fields in a Double.

If I set @State private var qty: Double = 0 then the form field, naturally, has a default of 0. But when adding a new item, I want that field to just be blank.

enter image description here

If I just remove the default and make it @State private var qty: Double, then an error is thrown.

enter image description here

Upvotes: 0

Views: 698

Answers (1)

azamsharp
azamsharp

Reputation: 20066

How about making the property optional?

struct ContentView: View {
    
    @State private var amount: Double?
    
    var body: some View {
        VStack {
            TextField("Amount", value: $amount, format: .number)
                .keyboardType(.decimalPad)
        }
        .padding()
    }
}

Upvotes: 1

Related Questions