0
0
iOS Swiftmobile~10 mins

Why forms capture structured data in iOS Swift - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a text field that captures user input.

iOS Swift
TextField("Enter name", text: $[1])
Drag options to blanks, or click blank then click option'
AuserName
BinputText
CtextValue
DnameInput
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not declared as @State
Forgetting the $ sign before the variable
2fill in blank
medium

Complete the code to add a button that prints the entered name when tapped.

iOS Swift
Button(action: {
  print([1])
}) {
  Text("Submit")
}
Drag options to blanks, or click blank then click option'
AnameInput
BinputText
CuserName
DtextValue
Attempts:
3 left
💡 Hint
Common Mistakes
Printing a variable that is not updated by the text field
Forgetting to use the correct variable name
3fill in blank
hard

Fix the error in the code to correctly declare a state variable for the form input.

iOS Swift
@State private var [1] = ""
Drag options to blanks, or click blank then click option'
AuserName
BinputText
CnameInput
DtextValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the TextField binding
Not initializing the variable
4fill in blank
hard

Fill both blanks to create a form with a text field and a button that prints the input.

iOS Swift
struct ContentView: View {
  @State private var [1] = ""

  var body: some View {
    VStack {
      TextField("Enter name", text: $[2])
      Button("Print") {
        print(userName)
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
AuserName
BinputText
CnameInput
DtextValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for state and binding
Forgetting the $ sign in the TextField binding
5fill in blank
hard

Fill all three blanks to create a form that captures a user's email and prints it on button tap.

iOS Swift
struct EmailForm: View {
  @State private var [1] = ""

  var body: some View {
    VStack {
      TextField("Enter email", text: $[2])
        .keyboardType([3])
      Button("Submit") {
        print(email)
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aemail
BinputText
CemailAddress
D.emailAddress
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for state and binding
Not setting the keyboard type for email input