0
0
iOS Swiftmobile~20 mins

Form container in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Container Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this SwiftUI form layout?
Consider this SwiftUI code for a form container. What will the user see when this view is rendered?
iOS Swift
import SwiftUI

struct ContentView: View {
  var body: some View {
    Form {
      Section(header: Text("User Info")) {
        TextField("Name", text: .constant("") )
        TextField("Email", text: .constant("") )
      }
      Section(header: Text("Preferences")) {
        Toggle("Subscribe", isOn: .constant(true))
      }
    }
  }
}
AA form with two sections: 'User Info' containing two text fields for Name and Email, and 'Preferences' containing a toggle switch labeled 'Subscribe'.
BA vertical stack with two text fields and a toggle, but no section headers or form styling.
CA list of buttons labeled 'Name', 'Email', and 'Subscribe' without input fields.
DAn empty view with no visible UI elements.
Attempts:
2 left
💡 Hint
Look at how Form and Section are used to group inputs with headers.
lifecycle
intermediate
2:00remaining
When does the SwiftUI Form update its state?
Given a Form with input fields bound to @State variables, when does the form update its UI to reflect changes?
iOS Swift
import SwiftUI

struct SettingsView: View {
  @State private var notificationsEnabled = false

  var body: some View {
    Form {
      Toggle("Enable Notifications", isOn: $notificationsEnabled)
      if notificationsEnabled {
        Text("Notifications are ON")
      } else {
        Text("Notifications are OFF")
      }
    }
  }
}
AThe form never updates automatically; state changes are ignored.
BThe form updates only when the view appears again after being dismissed.
CThe form updates only after a manual refresh triggered by the user.
DThe form updates its UI immediately when the toggle is changed by the user.
Attempts:
2 left
💡 Hint
Think about how @State works in SwiftUI.
navigation
advanced
2:00remaining
What happens when you embed a Form inside a NavigationView?
Consider this SwiftUI code snippet. What is the visible effect of embedding the Form inside a NavigationView with a title?
iOS Swift
import SwiftUI

struct ProfileView: View {
  var body: some View {
    NavigationView {
      Form {
        TextField("Username", text: .constant("") )
        SecureField("Password", text: .constant("") )
      }
      .navigationTitle("Profile")
    }
  }
}
AThe form appears without any navigation bar or title.
BThe form appears with a navigation bar at the top showing the title 'Profile'.
CThe form crashes because Form cannot be inside NavigationView.
DThe form appears but the title is shown at the bottom of the screen.
Attempts:
2 left
💡 Hint
NavigationView adds a navigation bar with a title.
📝 Syntax
advanced
2:00remaining
What error does this SwiftUI form code produce?
Examine this SwiftUI code snippet. What error will occur when compiling?
iOS Swift
import SwiftUI

struct TestView: View {
  var body: some View {
    Form {
      TextField("Enter", text: .constant("") )
      Section {
        Toggle("Enable", isOn: .constant(true))
      }
      Section(header: Text("Settings")) {
        Text("Extra")
      }
    }
  }
}
ASyntax error: 'Section' missing braces around its content.
BRuntime error: Toggle cannot be inside a Section.
CNo error; code compiles and runs fine.
DType error: TextField requires a binding to a @State variable, not a constant.
Attempts:
2 left
💡 Hint
Check the Section declaration syntax carefully.
🔧 Debug
expert
2:00remaining
Why does this form not update the toggle state?
This SwiftUI form has a toggle bound to a variable. Why does toggling it not change the UI?
iOS Swift
import SwiftUI

struct DebugView: View {
  var isOn = false

  var body: some View {
    Form {
      Toggle("Switch", isOn: $isOn)
    }
  }
}
ABecause the Form container disables toggles by default.
BBecause the Toggle label must be a Text view, not a string.
CBecause 'isOn' is a regular variable, not a @State property, so changes don't trigger UI updates.
DBecause the Toggle requires a Binding to an @ObservedObject, not a variable.
Attempts:
2 left
💡 Hint
Think about how SwiftUI tracks state changes.