Bird
Raised Fist0
Swiftprogramming~20 mins

@resultBuilder attribute in Swift - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the @resultBuilder attribute in Swift?
easy
A. To combine multiple expressions into a single result in a clean way
B. To declare a new type of variable
C. To handle errors automatically
D. To create a new thread for concurrent execution

Solution

  1. Step 1: Understand the role of @resultBuilder

    @resultBuilder is used to combine multiple code lines or expressions into one result, making code cleaner and easier to write.
  2. Step 2: Compare options with the purpose

    Options B, C, and D describe unrelated features. Only To combine multiple expressions into a single result in a clean way correctly describes the purpose of @resultBuilder.
  3. Final Answer:

    To combine multiple expressions into a single result in a clean way -> Option A
  4. Quick Check:

    @resultBuilder purpose = combine expressions [OK]
Hint: Remember: @resultBuilder combines code lines into one result [OK]
Common Mistakes:
  • Confusing @resultBuilder with error handling
  • Thinking it creates new variables
  • Assuming it manages concurrency
2. Which of the following is the correct way to declare a custom result builder in Swift?
easy
A. @resultBuilder class MyBuilder { func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } }
B. @resultBuilder struct MyBuilder { static func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } }
C. @resultBuilder func MyBuilder(_ components: Int...) -> Int { components.reduce(0, +) }
D. struct MyBuilder { func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } }

Solution

  1. Step 1: Identify correct @resultBuilder declaration

    A custom result builder must be declared as a struct or enum with the @resultBuilder attribute and static buildBlock method.
  2. Step 2: Check each option

    @resultBuilder struct MyBuilder { static func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } } correctly uses @resultBuilder on a struct and static buildBlock method. struct MyBuilder { func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } } misses @resultBuilder and static keyword. @resultBuilder func MyBuilder(_ components: Int...) -> Int { components.reduce(0, +) } wrongly applies @resultBuilder to a function. @resultBuilder class MyBuilder { func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } } uses a class, which is not allowed.
  3. Final Answer:

    @resultBuilder struct MyBuilder { static func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } } -> Option B
  4. Quick Check:

    Custom builder = struct + @resultBuilder + static buildBlock [OK]
Hint: Use @resultBuilder on struct with static buildBlock method [OK]
Common Mistakes:
  • Forgetting static keyword on buildBlock
  • Applying @resultBuilder to functions
  • Using class instead of struct or enum
3. Given the following code, what will be printed?
 @resultBuilder struct SumBuilder {
    static func buildBlock(_ components: Int...) -> Int {
        components.reduce(0, +)
    }
}

func sum(@SumBuilder _ content: () -> Int) -> Int {
    content()
}

let result = sum {
    2
    3
    5
}
print(result)
medium
A. Error: Missing return
B. 235
C. 0
D. 10

Solution

  1. Step 1: Understand buildBlock behavior

    The buildBlock method receives all integers (2, 3, 5) and sums them using reduce(0, +), resulting in 10.
  2. Step 2: Check function call and output

    The sum function calls content(), which triggers the builder and returns 10. print(result) outputs 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Sum of 2+3+5 = 10 [OK]
Hint: Sum builder adds all values inside closure [OK]
Common Mistakes:
  • Thinking values concatenate as strings
  • Expecting a syntax error
  • Missing that buildBlock returns sum
4. Identify the error in this custom result builder code:
 @resultBuilder struct MyBuilder {
    func buildBlock(_ components: String...) -> String {
        components.joined(separator: ", ")
    }
}

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

let result = buildString {
    "Hello"
    "World"
}
print(result)
medium
A. Missing return statement in buildBlock
B. Cannot use String type in result builder
C. buildBlock must be static
D. Function buildString should not have @MyBuilder

Solution

  1. Step 1: Check buildBlock method declaration

    In a result builder, buildBlock must be a static method. Here it is an instance method (missing static).
  2. Step 2: Verify other parts

    Return statement is present, String type is allowed, and function parameter correctly uses @MyBuilder.
  3. Final Answer:

    buildBlock must be static -> Option C
  4. Quick Check:

    buildBlock static keyword required [OK]
Hint: Always make buildBlock static in result builders [OK]
Common Mistakes:
  • Omitting static keyword on buildBlock
  • Confusing return statement presence
  • Thinking @resultBuilder restricts types
5. You want to create a custom result builder that builds a dictionary from key-value pairs written inside a closure. Which of the following implementations correctly uses @resultBuilder to achieve this?
hard
A. @resultBuilder struct DictBuilder { static func buildBlock(_ components: (String, Int)...) -> [String: Int] { var dict = [String: Int]() for (key, value) in components { dict[key] = value } return dict } } func buildDict(@DictBuilder _ content: () -> [String: Int]) -> [String: Int] { content() } let dict = buildDict { ("a", 1) ("b", 2) ("c", 3) } print(dict)
B. @resultBuilder struct DictBuilder { static func buildBlock(_ components: [String: Int]...) -> [String: Int] { components.reduce([:]) { $0.merging($1) { $1 } } } } func buildDict(@DictBuilder _ content: () -> [String: Int]) -> [String: Int] { content() } let dict = buildDict { ["a": 1] ["b": 2] ["c": 3] } print(dict)
C. @resultBuilder struct DictBuilder { static func buildBlock(_ components: String...) -> [String: Int] { [:] } } func buildDict(@DictBuilder _ content: () -> [String: Int]) -> [String: Int] { content() } let dict = buildDict { "a:1" "b:2" "c:3" } print(dict)
D. struct DictBuilder { static func buildBlock(_ components: (String, Int)...) -> [String: Int] { var dict = [String: Int]() for (key, value) in components { dict[key] = value } return dict } } func buildDict(@DictBuilder _ content: () -> [String: Int]) -> [String: Int] { content() } let dict = buildDict { ("a", 1) ("b", 2) ("c", 3) } print(dict)

Solution

  1. Step 1: Check for @resultBuilder attribute and static buildBlock

    @resultBuilder struct DictBuilder { static func buildBlock(_ components: (String, Int)...) -> [String: Int] { var dict = [String: Int]() for (key, value) in components { dict[key] = value } return dict } } func buildDict(@DictBuilder _ content: () -> [String: Int]) -> [String: Int] { content() } let dict = buildDict { ("a", 1) ("b", 2) ("c", 3) } print(dict) correctly declares @resultBuilder on a struct and has a static buildBlock method that combines tuples into a dictionary.
  2. Step 2: Verify code correctness and usage

    @resultBuilder struct DictBuilder { static func buildBlock(_ components: [String: Int]...) -> [String: Int] { components.reduce([:]) { $0.merging($1) { $1 } } } } func buildDict(@DictBuilder _ content: () -> [String: Int]) -> [String: Int] { content() } let dict = buildDict { ["a": 1] ["b": 2] ["c": 3] } print(dict) uses arrays of dictionaries which works but is less direct than tuples; @resultBuilder struct DictBuilder { static func buildBlock(_ components: String...) -> [String: Int] { [:] } } func buildDict(@DictBuilder _ content: () -> [String: Int]) -> [String: Int] { content() } let dict = buildDict { "a:1" "b:2" "c:3" } print(dict) returns empty dictionary ignoring input; struct DictBuilder { static func buildBlock(_ components: (String, Int)...) -> [String: Int] { var dict = [String: Int]() for (key, value) in components { dict[key] = value } return dict } } func buildDict(@DictBuilder _ content: () -> [String: Int]) -> [String: Int] { content() } let dict = buildDict { ("a", 1) ("b", 2) ("c", 3) } print(dict) misses @resultBuilder attribute, so it won't work as a builder.
  3. Final Answer:

    Option A correctly implements a dictionary builder using @resultBuilder -> Option A
  4. Quick Check:

    @resultBuilder + static buildBlock + tuple input = correct dictionary builder [OK]
Hint: Use tuples and static buildBlock with @resultBuilder for dictionary [OK]
Common Mistakes:
  • Forgetting @resultBuilder attribute
  • Returning empty or wrong type
  • Using non-static buildBlock
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard