Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a custom result builder named Builder.
Swift
resultBuilder [1] Builder { static func buildBlock(_ components: String...) -> String { components.joined(separator: ", ") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the '@' symbol before 'resultBuilder'.
Using 'struct' or 'class' instead of '@resultBuilder'.
✗ Incorrect
In Swift, a custom result builder is declared using the '@resultBuilder' attribute before the builder's name.
2fill in blank
mediumComplete the code to define the buildBlock method that combines components into a single string.
Swift
static func buildBlock(_ components: [1]) -> String { components.joined(separator: ", ") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '[String]' instead of 'String...'.
Using a single 'String' instead of variadic.
✗ Incorrect
The buildBlock method takes a variadic parameter of type String, written as 'String...'.
3fill in blank
hardFix the error in the buildIf method to correctly handle optional components.
Swift
static func buildIf(_ component: [1]) -> String { component ?? "" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-optional 'String' instead of 'String?'.
Using 'Int?' which is unrelated.
✗ Incorrect
The buildIf method takes an optional String parameter, written as 'String?'.
4fill in blank
hardFill both blanks to define a buildEither method for the first case in the result builder.
Swift
static func buildEither(first component: [1]) -> String { [2] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using optional type 'String?' instead of 'String'.
Omitting the 'return' keyword.
✗ Incorrect
The buildEither method takes a String and returns it using 'return component'.
5fill in blank
hardFill all three blanks to define a buildArray method that combines an array of components.
Swift
static func buildArray(_ components: [[1]]) -> [2] { components.[3](separator: ", ") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect types for the array or return type.
Using 'joined(separator:)' as a method name instead of 'joined'.
✗ Incorrect
The buildArray method takes an array of Strings and returns a String by joining components with ', '.