Bird
Raised Fist0
iOS Swiftmobile~10 mins

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

Choose your learning style10 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

Practice

(1/5)
1. Why do mobile apps use forms to capture user input as structured data?
easy
A. To organize input clearly and make it easy to use
B. To make the app run faster
C. To reduce the app size
D. To avoid using any user input

Solution

  1. Step 1: Understand the purpose of forms

    Forms collect user input in an organized way, grouping related fields together.
  2. Step 2: Recognize benefits of structured data

    Structured data helps the app process and validate input easily and correctly.
  3. Final Answer:

    To organize input clearly and make it easy to use -> Option A
  4. Quick Check:

    Forms = organize input clearly [OK]
Hint: Forms group data for easy use and validation [OK]
Common Mistakes:
  • Thinking forms only speed up the app
  • Believing forms reduce app size
  • Assuming forms avoid user input
2. Which Swift syntax correctly defines a struct to hold form data for a user with name and age?
easy
A. struct UserForm { var name: String; var age: Int }
B. class UserForm { name: String; age: Int }
C. struct UserForm { name = String; age = Int }
D. struct UserForm { var name; var age }

Solution

  1. Step 1: Identify correct struct syntax

    In Swift, structs use 'var' to declare properties with types, separated by semicolons or newlines.
  2. Step 2: Check each option

    struct UserForm { var name: String; var age: Int } uses correct syntax with 'var' and types. Others miss 'var', use wrong assignment, or omit types.
  3. Final Answer:

    struct UserForm { var name: String; var age: Int } -> Option A
  4. Quick Check:

    Swift struct properties need 'var' and type [OK]
Hint: Use 'var' and type inside struct for properties [OK]
Common Mistakes:
  • Omitting 'var' before property names
  • Using assignment '=' instead of type declaration
  • Confusing class and struct syntax
3. Given this Swift struct and code, what will print?
struct FormData {
  var email: String
  var subscribed: Bool
}

let data = FormData(email: "user@example.com", subscribed: true)
print(data.subscribed)
medium
A. user@example.com
B. true
C. false
D. Error: missing initializer

Solution

  1. Step 1: Understand struct initialization

    The struct FormData has two properties: email (String) and subscribed (Bool). The instance 'data' is created with email and subscribed values.
  2. Step 2: Check the print statement

    print(data.subscribed) outputs the Boolean value assigned, which is true.
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    Accessing struct property prints its value [OK]
Hint: Print struct property to see its stored value [OK]
Common Mistakes:
  • Confusing property names
  • Expecting email to print instead of subscribed
  • Thinking initialization is missing
4. What is wrong with this Swift struct for form data?
struct ContactForm {
  var phone: String
  var email: String
  init(phone: String) {
    self.phone = phone
  }
}
medium
A. Properties must be optional
B. Missing initializer for 'email' property
C. Structs cannot have initializers
D. Cannot use 'self' in init

Solution

  1. Step 1: Analyze the struct properties

    ContactForm has two properties: phone and email, both non-optional Strings.
  2. Step 2: Check the initializer

    The init only sets phone, but email is not initialized, causing a compile error.
  3. Final Answer:

    Missing initializer for 'email' property -> Option D
  4. Quick Check:

    All non-optional properties must be initialized [OK]
Hint: Initialize all non-optional properties in init [OK]
Common Mistakes:
  • Forgetting to initialize all properties
  • Thinking 'self' cannot be used in init
  • Believing structs cannot have initializers
5. You want to capture a form with optional phone and mandatory email. Which struct design correctly models this to capture structured data safely?
hard
A. struct Form { var phone: String; var email: String }
B. struct Form { var phone: String; var email: String? }
C. struct Form { var phone: String?; var email: String }
D. struct Form { var phone: String?; var email: String? }

Solution

  1. Step 1: Identify optional and mandatory fields

    Phone is optional, so it should be declared as an optional String (String?). Email is mandatory, so it must be a non-optional String.
  2. Step 2: Match struct design to requirements

    struct Form { var phone: String?; var email: String } correctly uses 'var phone: String?' and 'var email: String'. Others either make email optional or phone mandatory incorrectly.
  3. Final Answer:

    struct Form { var phone: String?; var email: String } -> Option C
  4. Quick Check:

    Optional for optional fields, non-optional for required [OK]
Hint: Use '?' for optional fields only [OK]
Common Mistakes:
  • Making mandatory fields optional
  • Forgetting to mark optional fields with '?'
  • Using all non-optional properties
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard