Complete the code to define a simple result builder attribute.
@resultBuilder struct [1] { static func buildBlock(_ components: String...) -> String { components.joined(separator: ", ") } }
The struct name defines the result builder. Here, MyBuilder is the custom builder name.
Complete the code to apply the result builder to a function.
func buildList(@[1] content: () -> String) -> String {
content()
}The function parameter uses the custom result builder MyBuilder to enable DSL syntax inside the closure.
Fix the error in the result builder usage by completing the code.
let result = buildList {
"Hello"
[1] "World"
}Inside the result builder closure, separate expressions with commas to build the combined result.
Fill both blanks to create a DSL-like syntax that builds a list of items.
let list = buildList {
[1] "Apple"
[2] "Banana"
}The blanks should be commas to separate items in the builder closure.
Fill all three blanks to define a result builder that combines strings with spaces and use it in a function.
@resultBuilder struct [1] { static func buildBlock(_ components: String...) -> String { components[2]separator: " "[3] } } func greet(@MyBuilder content: () -> String) -> String { content() }
The struct name is MyBuilder. The buildBlock method joins components with spaces using joined(separator: " ").