0
0
iOS Swiftmobile~20 mins

ViewBuilder for custom containers in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ViewBuilder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this SwiftUI custom container?
Consider this SwiftUI custom container using @ViewBuilder. What will be displayed when the ContentView is rendered?
iOS Swift
struct CustomStack<Content: View>: View {
  let content: () -> Content
  init(@ViewBuilder content: @escaping () -> Content) {
    self.content = content
  }
  var body: some View {
    VStack {
      Text("Header")
      content()
      Text("Footer")
    }
  }
}

struct ContentView: View {
  var body: some View {
    CustomStack {
      Text("Hello")
      Text("World")
    }
  }
}
AA horizontal stack showing: Header, Hello, World, Footer
BA vertical stack showing: Hello, World only
CA vertical stack showing: Header, Footer only
DA vertical stack showing: Header, Hello, World, Footer
Attempts:
2 left
💡 Hint
Remember that the content closure is inserted inside the VStack between Header and Footer.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a SwiftUI custom container with @ViewBuilder?
You want to create a custom container that accepts multiple child views using @ViewBuilder. Which code snippet is correct?
A
struct MyContainer&lt;Content: View&gt;: View {
  let content: () -&gt; Content
  init(@ViewBuilder content: @escaping () -&gt; Content) {
    self.content = content
  }
  var body: some View {
    VStack { content() }
  }
}
B
struct MyContainer&lt;Content: View&gt;: View {
  let content: Content
  init(@ViewBuilder content: Content) {
    self.content = content
  }
  var body: some View {
    VStack { content }
  }
}
C
struct MyContainer: View {
  let content: () -&gt; View
  init(@ViewBuilder content: @escaping () -&gt; View) {
    self.content = content
  }
  var body: some View {
    VStack { content() }
  }
}
D
struct MyContainer&lt;Content&gt;: View {
  let content: () -&gt; Content
  init(content: @ViewBuilder () -&gt; Content) {
    self.content = content
  }
  var body: some View {
    VStack { content() }
  }
}
Attempts:
2 left
💡 Hint
Look for the correct generic constraint and @escaping closure syntax.
lifecycle
advanced
2:00remaining
How does @ViewBuilder affect the lifecycle of child views in a custom container?
In SwiftUI, when you use @ViewBuilder in a custom container, how does it influence the creation and update of child views?
A@ViewBuilder allows multiple child views to be combined into a single View, and SwiftUI manages their lifecycle as a group.
B@ViewBuilder forces all child views to be recreated every time the container updates, ignoring state changes.
C@ViewBuilder converts child views into UIKit views immediately, bypassing SwiftUI's lifecycle.
D@ViewBuilder disables SwiftUI's view diffing, causing all child views to reload on every render.
Attempts:
2 left
💡 Hint
Think about how SwiftUI composes multiple views into one view tree.
navigation
advanced
2:00remaining
What happens if you use @ViewBuilder with conditional views inside a custom container?
Given a custom container using @ViewBuilder, what is the effect of including an if-else condition inside the content closure?
iOS Swift
struct ConditionalContainer<Content: View>: View {
  let content: () -> Content
  init(@ViewBuilder content: @escaping () -> Content) {
    self.content = content
  }
  var body: some View {
    VStack { content() }
  }
}

struct ContentView: View {
  @State private var showText = true
  var body: some View {
    ConditionalContainer {
      if showText {
        Text("Visible")
      } else {
        Text("Hidden")
      }
    }
  }
}
AThe container will always show both texts "Visible" and "Hidden" stacked vertically.
BThe container will crash because @ViewBuilder cannot handle if-else conditions.
CThe container will show either "Visible" or "Hidden" text depending on showText state, updating automatically.
DThe container will ignore the condition and show no text.
Attempts:
2 left
💡 Hint
Remember @ViewBuilder supports conditional view building.
🔧 Debug
expert
2:00remaining
Why does this custom container cause a compile error?
Examine this SwiftUI custom container code. Why does it fail to compile?
iOS Swift
struct BrokenContainer<Content: View>: View {
  let content: Content
  init(@ViewBuilder content: () -> Content) {
    self.content = content()
  }
  var body: some View {
    VStack {
      content
    }
  }
}

struct ContentView: View {
  var body: some View {
    BrokenContainer {
      Text("Hello")
      Text("World")
    }
  }
}
ABecause @ViewBuilder cannot be used in initializers.
BBecause Content is a single View, but the closure returns multiple views, causing a type mismatch.
CBecause content() is called without @escaping, causing a runtime error.
DBecause VStack cannot contain a variable named content.
Attempts:
2 left
💡 Hint
Think about what type the closure returns and what Content expects.