Bird
0
0

Which of the following is the correct syntax to create a basic TextField in SwiftUI bound to a state variable named name?

easy📝 Syntax Q12 of 15
iOS Swift - User Input and Forms
Which of the following is the correct syntax to create a basic TextField in SwiftUI bound to a state variable named name?
ATextField("Enter name", text: $name)
BTextField(text: name, placeholder: "Enter name")
CTextField("Enter name", text: name)
DTextField(text: $name, placeholder: "Enter name")
Step-by-Step Solution
Solution:
  1. Step 1: Recall SwiftUI TextField syntax

    The correct syntax uses a placeholder string first, then a binding to a state variable with a $ prefix.
  2. Step 2: Check each option

    TextField("Enter name", text: $name) matches the correct syntax: TextField("Enter name", text: $name). Others misuse parameter order or omit $.
  3. Final Answer:

    TextField("Enter name", text: $name) -> Option A
  4. Quick Check:

    Placeholder first, then binding with $ [OK]
Quick Trick: Use $ before state variable to bind TextField text [OK]
Common Mistakes:
  • Omitting $ before the state variable
  • Swapping placeholder and text parameters
  • Passing text variable without binding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes