0
0
Swiftprogramming~10 mins

BuildOptional and buildEither in 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 an optional view using BuildOptional.

Swift
var body: some View {
    VStack {
        Text("Hello")
        [1] {
            Text("Optional View")
        }
    }
}
Drag options to blanks, or click blank then click option'
AbuildEither
BbuildOptional
CbuildBlock
DbuildArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using buildEither instead of buildOptional for optional views.
Using buildBlock which is for multiple views, not optional ones.
2fill in blank
medium

Complete the code to choose the first view in a conditional using BuildEither.

Swift
var body: some View {
    Group {
        [1].first(
            Text("First View"),
            Text("Second View")
        )
    }
}
Drag options to blanks, or click blank then click option'
AbuildEither
BbuildOptional
CbuildBlock
DbuildArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using buildOptional when two views are involved.
Using buildBlock which is for multiple views, not conditional choices.
3fill in blank
hard

Fix the error in the code by choosing the correct builder function for an either-or view.

Swift
var body: some View {
    Group {
        [1].second(
            Text("Option A"),
            Text("Option B")
        )
    }
}
Drag options to blanks, or click blank then click option'
AbuildEither
BbuildBlock
CbuildOptional
DbuildArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using buildOptional which only handles optional views.
Using buildBlock which is for multiple views, not conditional choices.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that uses buildOptional and buildEither.

Swift
let views = [
    "optional": [1](Text("Optional View")),
    "either": [2].first(Text("First"), Text("Second"))
]
Drag options to blanks, or click blank then click option'
AbuildOptional
BbuildBlock
CbuildEither
DbuildArray
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up buildBlock with buildOptional or buildEither.
Using buildArray which is for arrays, not conditionals.
5fill in blank
hard

Fill all three blanks to build a dictionary with keys using buildEither and values using buildOptional.

Swift
let components = [
    [1].second("key1", "key2"): [2](Text("Value1")),
    [3].first("key3", "key4"): [2](Text("Value2"))
]
Drag options to blanks, or click blank then click option'
AbuildBlock
BbuildOptional
CbuildEither
DbuildArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using buildBlock for keys or values which is incorrect here.
Confusing buildArray with buildEither or buildOptional.