0
0
Swiftprogramming~20 mins

@resultBuilder attribute in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift ResultBuilder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code using @resultBuilder?
Consider this Swift code using a custom @resultBuilder named StringBuilder. What will be printed when buildString() is called?
Swift
@resultBuilder
struct StringBuilder {
    static func buildBlock(_ components: String...) -> String {
        components.joined(separator: ", ")
    }
}

func buildString(@StringBuilder content: () -> String) -> String {
    content()
}

let result = buildString {
    "Hello"
    "World"
    "Swift"
}
print(result)
A"[Hello, World, Swift]"
B"HelloWorldSwift"
C"Hello, World, Swift"
D"Hello World Swift"
Attempts:
2 left
💡 Hint
Look at how buildBlock joins the strings with a comma and space.
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of the @resultBuilder attribute in Swift?
Choose the best description of what @resultBuilder does in Swift.
AIt marks a function as asynchronous and able to run in the background.
BIt allows building complex values from multiple statements in a closure, combining them into one result.
CIt automatically generates getter and setter methods for properties.
DIt defines a protocol for handling errors in Swift code.
Attempts:
2 left
💡 Hint
Think about how SwiftUI uses @ViewBuilder to combine views.
🔧 Debug
advanced
2:30remaining
Why does this Swift code using @resultBuilder cause a compile error?
Examine this code snippet. Why does it fail to compile?
Swift
@resultBuilder
struct IntBuilder {
    static func buildBlock(_ components: Int...) -> Int {
        components.reduce(0, +)
    }
}

func sumInts(@IntBuilder content: () -> Int) -> Int {
    content()
}

let total = sumInts {
    1
    "2"
    3
}
print(total)
ABecause "2" is a String, not an Int, causing a type mismatch in the builder.
BBecause buildBlock must return an array, not a single Int.
CBecause the function sumInts is missing the @escaping attribute.
DBecause the @resultBuilder attribute can only be used with String types.
Attempts:
2 left
💡 Hint
Check the types of all values inside the closure.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a minimal @resultBuilder in Swift?
Select the code snippet that correctly defines a minimal @resultBuilder named BoolBuilder that combines multiple Bool values using logical AND.
A
@resultBuilder
struct BoolBuilder {
    static func buildBlock(_ components: Bool...) -> Bool {
        components.reduce(true) { $0 && $1 }
    }
}
B
@resultBuilder
struct BoolBuilder {
    func buildBlock(_ components: Bool...) -> Bool {
        components.allSatisfy { $0 }
    }
}
C
struct BoolBuilder {
    static func buildBlock(_ components: Bool...) -> Bool {
        components.reduce(true) { $0 && $1 }
    }
}
D
@resultBuilder
class BoolBuilder {
    static func buildBlock(_ components: Bool...) -> Bool {
        components.reduce(false) { $0 || $1 }
    }
}
Attempts:
2 left
💡 Hint
Remember @resultBuilder must be applied to a struct or enum with static buildBlock method.
🚀 Application
expert
3:00remaining
How many items are in the resulting array from this @resultBuilder usage?
Given this Swift code using a custom @resultBuilder named ArrayBuilder, how many elements does the result array contain?
Swift
@resultBuilder
struct ArrayBuilder {
    static func buildBlock<T>(_ components: [T]...) -> [T] {
        components.flatMap { $0 }
    }
    static func buildExpression<T>(_ expression: T) -> [T] {
        [expression]
    }
    static func buildExpression<T>(_ expression: [T]) -> [T] {
        expression
    }
}

func buildArray<T>(@ArrayBuilder content: () -> [T]) -> [T] {
    content()
}

let result = buildArray {
    1
    [2, 3]
    4
    []
    5
}
A4
B6
C7
D5
Attempts:
2 left
💡 Hint
Count how many elements each line contributes to the final array.