I am using a Bindable SwiftData as the value for my TextField:
@Bindable var myset: MySet
TextField(
"(myset.reps)",
value: myset.repBinding, formatter: NumberFormatter()
)
TextField(
"(myset.weight)",
value: myset.weightBinding, formatter: NumberFormatter()
)
and I am using an Extension for this since myset.weight and reps is a non optional Int:
extension MySet {
var weightBinding: Binding<Int?> {
Binding<Int?>(
get: { self.weight },
set: { value in
if let value {
self.weight = value
}
}
)
}
var repBinding: Binding<Int?> {
Binding<Int?>(
get: { self.reps },
set: { value in
if let value {
self.reps = value
}
}
)
}
}
Right now the way it works is when a new set is added it is set to 0 and 0 for its reps and weight since they are non optional, then the user has to click on the Textfield and delete the 0 to write their number, but what I want to happen is for that to happen as soon as the user clicks on the Textfield saving them the step of removing the 0. Is there any way to do this in SwiftUI?