0
0
Swiftprogramming~20 mins

Custom result builder declaration in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Result Builder 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 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())
A["apple", "banana", "cherry"]
B"applebanana cherry"
C["apple banana cherry"]
DSyntaxError
Attempts:
2 left
💡 Hint
Look at how the buildBlock method collects its string components into an array.
Predict Output
intermediate
2: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())
AOutput: 6
BRuntimeError: Cannot add String to Int
CTypeError: Cannot convert value of type 'String' to expected argument type 'Int'
DSyntaxError: Missing colon
Attempts:
2 left
💡 Hint
Check the types of the arguments passed to buildBlock.
🧠 Conceptual
advanced
2: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?
A
static func buildOptional(_ component: String?) -> [String] {
    if component != nil { return [component!] } else { return [""] }
}
B
static func buildOptional(_ component: String?) -> [String] {
    component.map { [$0] } ?? []
}
C
static func buildOptional(_ component: String?) -> String {
    component ?? ""
}
D
static func buildOptional(_ component: String?) -> [String] {
    [component!]
}
Attempts:
2 left
💡 Hint
Think about safely unwrapping optionals and returning an empty array if nil.
Predict Output
advanced
2: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))
AError: Missing buildOptional method
BOutput: 10
CError: Cannot convert value of type 'Int' to expected argument type '[Int]'
DOutput: 7
Attempts:
2 left
💡 Hint
The ternary selects the first branch (2) when condition is true. Sum 1 + 2 + 4.
🚀 Application
expert
3: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)
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Count all strings included by the builder, including conditionals and optionals.