0
0
Swiftprogramming~10 mins

Custom result builder declaration in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A@
Bclass
Cstruct
Denum
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the '@' symbol before 'resultBuilder'.
Using 'struct' or 'class' instead of '@resultBuilder'.
2fill in blank
medium

Complete 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'
AString
BInt
C[String]
DString...
Attempts:
3 left
💡 Hint
Common Mistakes
Using '[String]' instead of 'String...'.
Using a single 'String' instead of variadic.
3fill in blank
hard

Fix 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'
AString?
BString
COptional<String>
DInt?
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-optional 'String' instead of 'String?'.
Using 'Int?' which is unrelated.
4fill in blank
hard

Fill 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'
AString
Bcomponent
CString?
Dreturn component
Attempts:
3 left
💡 Hint
Common Mistakes
Using optional type 'String?' instead of 'String'.
Omitting the 'return' keyword.
5fill in blank
hard

Fill 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'
AString
Cjoined
Djoined(separator:)
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'.