0
0
iOS Swiftmobile~20 mins

@Binding for child communication in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binding Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when a child view modifies a @Binding variable?

Consider a SwiftUI parent view passing a @State variable as a @Binding to a child view. What is the effect when the child view modifies this @Binding variable?

iOS Swift
struct ParentView: View {
  @State private var count = 0
  var body: some View {
    ChildView(count: $count)
  }
}

struct ChildView: View {
  @Binding var count: Int
  var body: some View {
    Button("Increment") {
      count += 1
    }
  }
}
AThe parent view's count variable updates and the UI refreshes accordingly.
BOnly the child view's count updates; the parent view remains unchanged.
CThe count variable does not change because @Binding is read-only.
DThe app crashes because @Binding cannot be modified in the child.
Attempts:
2 left
💡 Hint

Think about how @Binding connects child and parent state in SwiftUI.

📝 Syntax
intermediate
1:30remaining
Identify the correct way to declare a @Binding variable in a child view

Which of the following is the correct syntax to declare a @Binding variable named isOn of type Bool in a SwiftUI child view?

A@State var isOn: Bool
Bvar isOn: Binding<Bool>
C@Binding var isOn: Bool
Dlet isOn: Bool
Attempts:
2 left
💡 Hint

Remember that @Binding is a property wrapper used to declare a binding variable.

lifecycle
advanced
2:00remaining
When does a SwiftUI view update after a @Binding variable changes?

In SwiftUI, after a child view modifies a @Binding variable, when does the parent view re-render?

AThe parent view never re-renders automatically from @Binding changes.
BAfter the current run loop completes and SwiftUI schedules a view update.
CImmediately after the @Binding variable changes, before the current run loop ends.
DOnly when the parent view explicitly calls <code>setNeedsDisplay()</code>.
Attempts:
2 left
💡 Hint

Think about how SwiftUI batches view updates for performance.

🔧 Debug
advanced
2:30remaining
Why does this child view fail to update the parent state via @Binding?

Given the code below, why does tapping the button not update the parent view's text?

iOS Swift
struct ParentView: View {
  @State private var text = "Hello"
  var body: some View {
    ChildView(text: text)
  }
}

struct ChildView: View {
  @Binding var text: String
  var body: some View {
    Button("Change") {
      text = "World"
    }
  }
}
AThe parent passes a value instead of a binding; it should pass $text.
BThe child view's @Binding variable is missing the @State wrapper.
CThe button action is not called because the label is incorrect.
DThe parent view's text variable is declared with @State, which is incompatible with @Binding.
Attempts:
2 left
💡 Hint

Check how the parent passes the variable to the child.

🧠 Conceptual
expert
3:00remaining
What is the main advantage of using @Binding for child communication in SwiftUI?

Why is @Binding preferred for passing data from a parent to a child view when the child needs to modify the parent's state?

AIt disables the parent's state updates while the child is active to avoid conflicts.
BIt copies the parent's state to the child, preventing accidental changes to the parent.
CIt forces the child to manage its own independent state, improving modularity.
DIt creates a two-way connection allowing the child to update the parent's state directly, keeping UI in sync.
Attempts:
2 left
💡 Hint

Think about how data flows and updates between views in SwiftUI.