The correct syntax is SecureField("Password", text: $password).
Why should you use SecureField instead of TextField for passwords?
ATo automatically save the password
BTo enable spell check
CTo allow multiline input
DTo hide the password characters on screen
✗ Incorrect
SecureField hides the password characters to protect user privacy.
Which of these is NOT a feature of SecureField?
AMasking input characters
BShowing typed text openly
CBinding to a state variable
DDisplaying placeholder text
✗ Incorrect
SecureField does NOT show typed text openly; it masks it.
Explain how to create a password input field in SwiftUI using SecureField.
Think about how you bind text and how the field hides input.
You got /4 concepts.
Why is it important to use SecureField for password inputs instead of a regular TextField?
Consider what happens when someone looks at the screen while typing.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using SecureField in a SwiftUI app?
easy
A. To display a multiline text input
B. To hide the text input, typically for passwords
C. To create a button that submits a form
D. To show a label with static text
Solution
Step 1: Understand SecureField's role
SecureField is designed to hide user input, making it ideal for passwords.
Step 2: Compare with other UI elements
Unlike TextField, SecureField masks the input characters for privacy.
Final Answer:
To hide the text input, typically for passwords -> Option B
Quick Check:
SecureField hides input = B [OK]
Hint: SecureField always hides input text for privacy [OK]
Common Mistakes:
Confusing SecureField with TextField
Thinking SecureField shows visible text
Using SecureField for buttons or labels
2. Which of the following is the correct syntax to create a SecureField bound to a @State variable named password with placeholder "Enter password"?
easy
A. SecureField("Enter password", text: $password)
B. SecureField(text: $password, placeholder: "Enter password")
C. SecureField("Enter password", binding: password)
D. SecureField(text: password, placeholder: "Enter password")
Solution
Step 1: Recall SecureField initializer
The correct initializer is SecureField(_ titleKey: String, text: Binding<String>).
Step 2: Match syntax with variable
Binding uses $password to connect the state variable.
Final Answer:
SecureField("Enter password", text: $password) -> Option A
Quick Check:
SecureField("text", text: $variable) = D [OK]
Hint: Use $ before state variable for binding in SecureField [OK]
Common Mistakes:
Swapping parameter order
Missing $ for binding
Using wrong parameter names
3. What will be displayed on screen when this SwiftUI code runs?
struct ContentView: View {
@State private var password = ""
var body: some View {
SecureField("Password", text: $password)
.padding()
.border(Color.gray)
}
}
medium
A. A text input box showing typed characters as dots or bullets
B. A plain text label showing the word "Password"
C. A button labeled "Password"
D. An empty screen with no visible UI
Solution
Step 1: Identify UI element
The code uses SecureField with placeholder "Password" and binds to a state variable.
Step 2: Understand SecureField behavior
It shows a text input box that masks typed characters as dots or bullets.
Final Answer:
A text input box showing typed characters as dots or bullets -> Option A
Quick Check:
SecureField input masked = A [OK]
Hint: SecureField input is always masked visually [OK]
Common Mistakes:
Thinking placeholder text is always visible
Confusing SecureField with Text label
Expecting a button instead of input
4. Identify the error in this SwiftUI code snippet:
@State private var password = ""
var body: some View {
SecureField("Enter password", text: password)
}
medium
A. Password variable should be a constant
B. SecureField cannot be used inside body
C. Placeholder text must be empty string
D. Missing $ before password in binding
Solution
Step 1: Check binding syntax
The text parameter requires a binding, which needs $password, not just password.
Step 2: Understand @State usage
@State variables are accessed with $ to create bindings for UI elements.
Final Answer:
Missing $ before password in binding -> Option D
Quick Check:
Binding needs $ prefix = C [OK]
Hint: Always use $ before @State variable for binding in SecureField [OK]
Common Mistakes:
Omitting $ causing compile error
Thinking SecureField can't be in body
Misunderstanding placeholder requirements
5. You want to create a password input field that shows a hint "At least 8 characters" below the SecureField. Which SwiftUI code snippet correctly achieves this?
hard
A. SecureField("Password", text: $password).hint("At least 8 characters")
B. SecureField("Password", text: $password).placeholder("At least 8 characters")
C. VStack {
SecureField("Password", text: $password)
Text("At least 8 characters").font(.footnote).foregroundColor(.gray)
}
D. Text("At least 8 characters")
SecureField("Password", text: $password)
Solution
Step 1: Understand layout for hint text
Use a VStack to stack SecureField and hint Text vertically.
Step 2: Style hint text appropriately
Apply smaller font and gray color to hint text for subtlety.
Step 3: Check other options
Options B and C use non-existent modifiers; D places Text above SecureField, not below.
Final Answer:
VStack {
SecureField("Password", text: $password)
Text("At least 8 characters").font(.footnote).foregroundColor(.gray)
} -> Option C
Quick Check:
Use VStack for hint below SecureField = A [OK]
Hint: Stack SecureField and hint Text in VStack for vertical layout [OK]
Common Mistakes:
Using invalid modifiers like .placeholder or .hint
Placing hint text above SecureField
Not styling hint text for clarity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.