Challenge - 5 Problems
Swift ResultBuilder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Look at how buildBlock joins the strings with a comma and space.
✗ Incorrect
The buildBlock function joins all string components with ", ". So the output is the three strings separated by commas and spaces.
🧠 Conceptual
intermediate1:30remaining
What is the main purpose of the @resultBuilder attribute in Swift?
Choose the best description of what @resultBuilder does in Swift.
Attempts:
2 left
💡 Hint
Think about how SwiftUI uses @ViewBuilder to combine views.
✗ Incorrect
@resultBuilder lets you write multiple statements inside a closure and combines them into a single value, making code more readable and declarative.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the types of all values inside the closure.
✗ Incorrect
The closure includes a String "2" where an Int is expected, causing a type mismatch error during compilation.
📝 Syntax
advanced2: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.Attempts:
2 left
💡 Hint
Remember @resultBuilder must be applied to a struct or enum with static buildBlock method.
✗ Incorrect
Option A correctly uses @resultBuilder on a struct with a static buildBlock method that combines Bool values with AND. Option A's buildBlock is not static. Option A lacks @resultBuilder. Option A uses a class and OR logic, which is not minimal AND builder.
🚀 Application
expert3: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 }
Attempts:
2 left
💡 Hint
Count how many elements each line contributes to the final array.
✗ Incorrect
Each single value is wrapped into an array of one element by buildExpression. The array [2, 3] is passed as is. The empty array contributes zero elements. So total elements: 1 + 2 + 1 + 0 + 1 = 5 elements.