Home > Software design >  How do you set a Double to nil?
How do you set a Double to nil?

Time:03-21

The following code gives me input A as "0" in the textfield.

I would like it to say "Input A" in the standard gray type.

If you select the 0 and delete it, it will show the "Input A."

No matter what I try I always end up with an error.

Is there a way for this to work?

import SwiftUI

struct ContentView: View {
    
    @State private var lineOneA: Double = Double()
    @State private var lineThreeA: Double = Double()
    
    var body: some View {
        let lineOneAProxy = Binding<String>(
            get: { String(format: "%.0f", Double(self.lineOneA)) },
            set: { if let value = NumberFormatter().number(from: $0) {
                self.lineOneA = value.doubleValue}
            }
        )
        return VStack(spacing: 10) {
            HStack() {
                Text("Line One ->")
                    .foregroundColor(Color(.gray))
                TextField("INPUT A", text: lineOneAProxy) {
                    
// ----> How do I set this to nil in order to get rid of the 0 and show "INPUT" instead? This goes for all three inputs.

                    if lineOneA > 0 {
                        lineThreeA = round(10000*(100/(lineOneA 100)))/10000
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

CodePudding user response:

This compiles without errors for me, so not sure what the errors are.

The purpose of TextField is to bind to a string. If the string is empty (not the same as nil - empty is ""), it will show the placeholder text.

There are ways to constrain the type of input on a textfield (SwiftUI - How to create TextField that only accepts numbers)

What I did here is add an if-statement: if the Double is 0, it will return a blank string (making the placeholder show) instead of formatting the 0 and displaying that.

import SwiftUI

struct ContentView: View {
    
    @State private var lineOneA: Double = Double()
    @State private var lineThreeA: Double = Double()
    
    var body: some View {
        let lineOneAProxy = Binding<String>(
            get: {
                // this bit:
                if self.lineOneA == 0 {
                    return ""
                } else {
                    return String(format: "%.0f", Double(self.lineOneA))
                }
            },
            set: {
                if let value = NumberFormatter().number(from: $0) {
                    self.lineOneA = value.doubleValue
                }
            }
        )
        
        return VStack(spacing: 10) {
            HStack() {
                Text("Line One ->")
                    .foregroundColor(Color(.gray))
                TextField("INPUT A", text: lineOneAProxy) {
                    
// ----> How do I set this to nil in order to get rid of the 0 and show "INPUT" instead? This goes for all three inputs.

                    if lineOneA > 0 {
                        lineThreeA = round(10000*(100/(lineOneA 100)))/10000
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Also, as a general rule you should avoid having logic in the var body computed property, so these custom bindings are not optimal. Consider making the state var a String and doing the conversion when you need the double.

  • Related