0
0
iOS Swiftmobile~10 mins

Toggle switch in iOS Swift - Interactive Code Practice

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

Complete 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'
AisEnabled
BisOn
CtoggleState
DfeatureToggle
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared as @State.
Forgetting the $ sign before the variable name.
2fill in blank
medium

Complete 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'
AtoggleState
BisOn
CswitchValue
DfeatureEnabled
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the one bound to the toggle.
Not marking the variable with @State.
3fill in blank
hard

Fix 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'
A$toggleState
BtoggleState
C*toggleState
D&toggleState
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the variable without $ causes a type error.
Using & or * are not valid in SwiftUI bindings.
4fill in blank
hard

Fill 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'
AdarkModeEnabled
BdarkMode
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in declaration and binding.
Forgetting the $ in the binding.
5fill in blank
hard

Fill 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'
AairplaneMode
DisOn
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in different places.
Not using the variable in the text ternary expression.