Challenge - 5 Problems
Swift Result Builder 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 a custom result builder?
Consider the following Swift code that uses a custom result builder named
StringListBuilder. What will be printed when print(buildList()) is called?Swift
import Foundation @resultBuilder struct StringListBuilder { static func buildBlock(_ components: String...) -> [String] { return components } } func buildList() -> [String] { StringListBuilder.buildBlock( "apple", "banana", "cherry" ) } print(buildList())
Attempts:
2 left
💡 Hint
Look at how the
buildBlock method collects its string components into an array.✗ Incorrect
The
buildBlock method takes a variable number of String arguments and returns them as an array. So the function buildList() returns an array of strings, which is printed as ["apple", "banana", "cherry"].❓ Predict Output
intermediate2:00remaining
What error does this Swift custom result builder code produce?
Given this Swift code with a custom result builder, what error will occur when compiling?
Swift
@resultBuilder struct IntBuilder { static func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } } func buildSum() -> Int { IntBuilder.buildBlock( 1, 2, "3" ) } print(buildSum())
Attempts:
2 left
💡 Hint
Check the types of the arguments passed to
buildBlock.✗ Incorrect
The
buildBlock expects only Int arguments, but a String "3" is passed, causing a compile-time type error.🧠 Conceptual
advanced2:00remaining
Which option correctly implements a custom result builder that supports optional components?
You want to create a custom result builder that can handle optional strings, including them only if they are non-nil. Which implementation of
buildOptional is correct?Attempts:
2 left
💡 Hint
Think about safely unwrapping optionals and returning an empty array if nil.
✗ Incorrect
Option B safely unwraps the optional and returns an array with the string if non-nil, or an empty array if nil. This matches the expected behavior for
buildOptional in a result builder that produces arrays.❓ Predict Output
advanced2:00remaining
What is the output of this Swift custom result builder code?
Given this Swift code with a custom result builder, what will be printed when
print(buildNumber(condition: true)) is called?Swift
@resultBuilder struct NumberBuilder { static func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) } static func buildEither(first component: Int) -> Int { component } static func buildEither(second component: Int) -> Int { component } } func buildNumber(condition: Bool) -> Int { NumberBuilder.buildBlock( 1, condition ? NumberBuilder.buildEither(first: 2) : NumberBuilder.buildEither(second: 3), 4 ) } print(buildNumber(condition: true))
Attempts:
2 left
💡 Hint
The ternary selects the first branch (2) when condition is true. Sum 1 + 2 + 4.
✗ Incorrect
The ternary expression selects
NumberBuilder.buildEither(first: 2), returning 2. buildBlock(1, 2, 4) sums to 7 via reduce(0, +).🚀 Application
expert3:00remaining
How many items are in the resulting array from this custom result builder usage?
Given this Swift code with a custom result builder that supports conditionals and optionals, how many strings are in the array returned by
buildMenu(showDessert: true)?Swift
@resultBuilder struct MenuBuilder { static func buildBlock(_ components: [String]...) -> [String] { components.flatMap { $0 } } static func buildOptional(_ component: [String]?) -> [String] { component ?? [] } static func buildEither(first component: [String]) -> [String] { component } static func buildEither(second component: [String]) -> [String] { component } } func buildMenu(showDessert: Bool) -> [String] { MenuBuilder.buildBlock( ["Salad", "Soup"], showDessert ? MenuBuilder.buildEither(first: ["Cake"]) : MenuBuilder.buildEither(second: []), MenuBuilder.buildOptional(nil) ) } let menu = buildMenu(showDessert: true) print(menu.count)
Attempts:
2 left
💡 Hint
Count all strings included by the builder, including conditionals and optionals.
✗ Incorrect
The builder combines ["Salad", "Soup"] plus ["Cake"] because showDessert is true, and
buildOptional(nil) returns []. So total items are 3.