Challenge - 5 Problems
Swift BuildOptional & BuildEither 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 buildOptional?
Consider this Swift code snippet using buildOptional in a result builder. What will be printed?
Swift
import Foundation @resultBuilder struct TestBuilder { static func buildOptional(_ component: String?) -> String { return component ?? "No value" } static func buildBlock(_ component: String) -> String { return component } } func test() -> String { @TestBuilder var result: String { if Bool.random() { "Hello" } } return result } print(test())
Attempts:
2 left
💡 Hint
buildOptional handles optional components in result builders, returning a default if nil.
✗ Incorrect
The buildOptional function receives an optional String. If the if condition is true, it returns "Hello"; otherwise, nil. The buildOptional returns "No value" when nil is passed, so the output depends on the random condition.
❓ Predict Output
intermediate2:00remaining
What does this Swift code print using buildEither?
Given this Swift code using buildEither in a result builder, what will be the output?
Swift
import Foundation @resultBuilder struct EitherBuilder { static func buildEither(first component: String) -> String { return "First: " + component } static func buildEither(second component: String) -> String { return "Second: " + component } static func buildBlock(_ component: String) -> String { return component } } func test(flag: Bool) -> String { @EitherBuilder var result: String { if flag { "Option A" } else { "Option B" } } return result } print(test(flag: true)) print(test(flag: false))
Attempts:
2 left
💡 Hint
buildEither distinguishes between first and second branches in conditional statements inside result builders.
✗ Incorrect
When flag is true, buildEither(first:) is called with "Option A", returning "First: Option A". When flag is false, buildEither(second:) is called with "Option A", returning "Second: Option A". So the output is "First: Option A\nSecond: Option A".
🔧 Debug
advanced2:30remaining
Why does this Swift code using buildEither cause a compilation error?
Examine this Swift code snippet using buildEither. Why does it fail to compile?
Swift
import Foundation @resultBuilder struct MyBuilder { static func buildEither(first component: Int) -> Int { return component } static func buildEither(second component: String) -> String { return component } static func buildBlock(_ component: Any) -> Any { return component } } func test(flag: Bool) -> Any { @MyBuilder var result: Any { if flag { 42 } else { "Answer" } } return result }
Attempts:
2 left
💡 Hint
Result builder functions must have consistent return types to compile.
✗ Incorrect
The buildEither methods return different types (Int and String), which causes the compiler to fail because it cannot unify the return type. Result builders require consistent types for buildEither methods.
🧠 Conceptual
advanced1:30remaining
How does buildOptional affect the output in a Swift result builder?
In Swift result builders, what role does buildOptional play when an optional component is present?
Attempts:
2 left
💡 Hint
Think about how optional values are handled inside result builders.
✗ Incorrect
buildOptional is called when an optional component is present. It unwraps the optional and can provide a default or alternative value if the optional is nil, allowing the builder to continue gracefully.
❓ Predict Output
expert3:00remaining
What is the output of this complex Swift result builder using buildEither and buildOptional?
Analyze this Swift code using both buildEither and buildOptional in a result builder. What will it print?
Swift
import Foundation @resultBuilder struct ComplexBuilder { static func buildBlock(_ components: String...) -> String { components.joined(separator: ", ") } static func buildEither(first component: String) -> String { "First(\(component))" } static func buildEither(second component: String) -> String { "Second(\(component))" } static func buildOptional(_ component: String?) -> String { component ?? "None" } } func complexTest(flag1: Bool, flag2: Bool) -> String { @ComplexBuilder var result: String { if flag1 { "A" } else { "B" } if flag2 { "C" } } return result } print(complexTest(flag1: true, flag2: true)) print(complexTest(flag1: false, flag2: false))
Attempts:
2 left
💡 Hint
buildEither wraps the if-else branches, buildOptional handles the optional second if.
✗ Incorrect
For flag1 true, buildEither(first:) wraps "A" as "First(A)"; flag2 true includes "C". For flag1 false, buildEither(second:) wraps "B" as "Second(B)"; flag2 false means the second if is nil, so buildOptional returns "None". The joined result reflects these.