0
0
iOS Swiftmobile~10 mins

Why state drives reactive UI updates in iOS Swift - Test Your Understanding

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

Complete the code to declare a state variable in SwiftUI.

iOS Swift
@State private var count: [1] = 0
Drag options to blanks, or click blank then click option'
AInt
BString
CBool
DDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using String instead of Int for a number variable.
2fill in blank
medium

Complete the code to update the state variable when the button is pressed.

iOS Swift
Button(action: {
  count [1] 1
}) {
  Text("Increment")
}
Drag options to blanks, or click blank then click option'
A+=
B-=
C*=
D/=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= which subtracts instead of adds.
3fill in blank
hard

Fix the error in the code to display the count value in the Text view.

iOS Swift
Text("Count: \( [1] )")
Drag options to blanks, or click blank then click option'
Aself.count()
B"count"
Ccount()
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses like a function call.
4fill in blank
hard

Fill both blanks to create a reactive view that updates when the state changes.

iOS Swift
struct ContentView: View {
  @State private var count: [1] = 0

  var body: some View {
    VStack {
      Text("Count: \(count)")
      Button(action: {
        count [2] 1
      }) {
        Text("Add")
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
AInt
B+=
C-=
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using String type for count.
Using -= which decreases the count.
5fill in blank
hard

Fill all three blanks to create a SwiftUI view that shows a toggle and text reflecting its state.

iOS Swift
struct ToggleView: View {
  @State private var isOn: [1] = false

  var body: some View {
    VStack {
      Toggle(isOn: $[2]) {
        Text("Switch")
      }
      Text(isOn ? [3] : "Off")
    }
  }
}
Drag options to blanks, or click blank then click option'
ABool
BisOn
C"On"
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using Int instead of Bool.
Using wrong variable name for binding.
Not using quotes for the "On" string.