I would like my iOS SwiftUI TextField to display the placeholder "Enter an integer:", but the TextField displays 0 (the value of the var value). The placeholder "Enter an integer:" does appear when the user deletes the 0. How can I get the placeholder "Enter an integer:" to appear without making the user delete the 0 first? Thank you in advance. Xcode 11.6 on macOS Catalina 10.15.6.
struct ContentView: View {
@State private var value: Int = 0;
@State private var text: String = "";
var body: some View {
VStack {
TextField(
"Enter an integer:",
value: $value,
formatter: NumberFormatter(),
onCommit: {self.text = "The product is \(2 * self.value).";}
)
.keyboardType(.numbersAndPunctuation)
.textFieldStyle(RoundedBorderTextFieldStyle())
Text(text)
}
.padding([.leading, .trailing], 16)
}
}
