Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a toggle switch in SwiftUI.
iOS Swift
Toggle(isOn: $[1]) { Text("Enable feature") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared as @State.
Forgetting the $ sign before the variable name.
✗ Incorrect
The toggle needs a binding to a @State variable, here named toggleState, to track its on/off state.
2fill in blank
mediumComplete the code to declare a state variable for the toggle switch.
iOS Swift
@State private var [1] = false Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the one bound to the toggle.
Not marking the variable with @State.
✗ Incorrect
The variable toggleState is used to track the toggle's state and must be declared with @State.
3fill in blank
hardFix the error in the toggle binding by completing the code.
iOS Swift
Toggle(isOn: [1]) { Text("Notifications") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable without $ causes a type error.
Using & or * are not valid in SwiftUI bindings.
✗ Incorrect
The toggle requires a binding, so the variable must be prefixed with $ to pass a binding.
4fill in blank
hardFill both blanks to create a toggle with a label and a state variable.
iOS Swift
@State private var [1] = false Toggle(isOn: $[2]) { Text("Dark Mode") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in declaration and binding.
Forgetting the $ in the binding.
✗ Incorrect
The state variable name must be consistent in declaration and binding. Here, darkModeEnabled is used.
5fill in blank
hardFill all three blanks to create a toggle that updates a text label based on its state.
iOS Swift
@State private var [1] = false var body: some View { VStack { Toggle(isOn: $[2]) { Text("Airplane Mode") } Text([3] ? "On" : "Off") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in different places.
Not using the variable in the text ternary expression.
✗ Incorrect
The same state variable airplaneMode is declared, bound to the toggle, and used to show the text status.