Complete the code to create a secure password input field using SwiftUI.
SecureField("Enter password", text: $[1])
The SecureField requires a binding to a String variable that stores the password. Here, password is the correct variable name.
Complete the code to declare a state variable for the password in SwiftUI.
@State private var [1] = ""
The password input needs a @State variable to store its value. Naming it password is clear and standard.
Fix the error in the SecureField declaration by completing the code.
SecureField("Password", text: [1])
In SwiftUI, to bind a variable to a control, you must use the $ prefix. So $password is correct.
Fill both blanks to create a SwiftUI view with a SecureField and a Text label showing password length.
struct ContentView: View {
@State private var [1] = ""
var body: some View {
VStack {
SecureField("Password", text: $[2])
Text("Length: \([1].count)")
}
}
}Both blanks refer to the same password variable. The SecureField binds to $password, and the Text shows the count of characters in password.
Fill all three blanks to create a SwiftUI view with a SecureField, a Text label showing password length, and a Button to clear the password.
struct ContentView: View {
@State private var [1] = ""
var body: some View {
VStack {
SecureField("Password", text: $[2])
Text("Length: \([2].count)")
Button("Clear") {
[3] = ""
}
}
}
}All blanks use the same variable password. The SecureField binds to $password, the Text shows password.count, and the Button clears password.