Challenge - 5 Problems
Form Container Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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)) } } } }
Attempts:
2 left
💡 Hint
Look at how Form and Section are used to group inputs with headers.
✗ Incorrect
The Form container in SwiftUI groups inputs into sections with headers. TextField and Toggle inside sections create labeled input controls. This code produces a form with two labeled sections and appropriate input controls.
❓ lifecycle
intermediate2: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") } } } }
Attempts:
2 left
💡 Hint
Think about how @State works in SwiftUI.
✗ Incorrect
In SwiftUI, @State variables trigger the view to re-render immediately when their values change. The form updates its UI instantly to reflect the new state.
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") } } }
Attempts:
2 left
💡 Hint
NavigationView adds a navigation bar with a title.
✗ Incorrect
Embedding a Form inside NavigationView adds a navigation bar at the top. The navigationTitle modifier sets the title text visible in that bar.
📝 Syntax
advanced2: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") } } } }
Attempts:
2 left
💡 Hint
Check the Section declaration syntax carefully.
✗ Incorrect
The Section with header 'Settings' lacks braces {} around its content. This causes a syntax error because Section expects a closure for its content.
🔧 Debug
expert2: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) } } }
Attempts:
2 left
💡 Hint
Think about how SwiftUI tracks state changes.
✗ Incorrect
In SwiftUI, only @State properties trigger UI updates when changed. A plain variable does not provide a binding that updates the view.