I have the following code:
struct Food {
var name: String
var energy: Float = 0
var water: Float = 0
}
struct FoodItemView: View {
@State var newFoodItem = Food(name: "")
private var numberFormatter = NumberFormatter()
var body: some View {
Form {
Section(header: Text("Basics").fontWeight(.bold)) {
TextField("Name", text: $newFoodItem.name)
TextField("Energy (C)", value: $newFoodItem.energy, formatter: numberFormatter)
.keyboardType(.numberPad)
TextField("Water (g)", value: $newFoodItem.water, formatter: numberFormatter)
}
}
}
How do I make it so that, the value displayed in my TextFields would be an empty string if the bound value is 0, rather than explicitly displaying the 0?
Set .zeroSymbol
property of NumberFormatter
.
struct FoodItemView: View {
@State var newFoodItem = Food(name: "")
private var numberFormatter = NumberFormatter()
init() {
numberFormatter.zeroSymbol = "" //<< Here
}
// Other Code