Bird
0
0

Which approach correctly implements this behavior?

hard📝 Application Q15 of 15
iOS Swift - User Input and Forms
You want to create a SwiftUI form where the user enters their email in a TextField. You want to validate the input so that the submit button is only enabled when the email contains an '@' character. Which approach correctly implements this behavior?
AUse a @State variable for email, bind it to TextField, and disable the button with .disabled(!email.contains("@"))
BUse a constant String for email and enable the button always
CBind TextField to a local variable and enable button only if email length > 0
DUse @State variable for email but do not bind it to TextField; enable button always
Step-by-Step Solution
Solution:
  1. Step 1: Understand state binding and validation

    Binding a @State variable to TextField allows live updates. Validation can check if email contains '@'.
  2. Step 2: Control button enabled state

    Using .disabled(!email.contains("@")) disables the button unless email has '@', enforcing validation.
  3. Final Answer:

    Use a @State variable for email, bind it to TextField, and disable the button with .disabled(!email.contains("@")) -> Option A
  4. Quick Check:

    Bind state, validate with contains('@'), disable button accordingly [OK]
Quick Trick: Bind email state and disable button if '@' missing [OK]
Common Mistakes:
  • Not binding TextField to a @State variable
  • Using constant or local variables for input
  • Enabling button without validation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes