Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a function parameter as an autoclosure.
Swift
func logIfTrue(_ predicate: [1]) { if predicate() { print("Condition is true") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add '@autoclosure' before the closure type.
Using just 'Bool' instead of a closure type.
Using '@escaping' which is not needed here.
✗ Incorrect
The @autoclosure attribute automatically wraps an expression into a closure, so the parameter type must be '@autoclosure () -> Bool'.
2fill in blank
mediumComplete the code to call the function with an autoclosure parameter.
Swift
logIfTrue([1] > 5)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a Boolean literal directly without an expression.
Passing a value that is not a Boolean expression.
✗ Incorrect
You can pass an expression like '3 + 4' directly to an autoclosure parameter; it will be wrapped automatically.
3fill in blank
hardFix the error in the function declaration to correctly use @autoclosure.
Swift
func check(_ condition: [1]) { if condition() { print("Checked true") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Bool' instead of a closure type.
Using '@escaping' which is not needed here.
✗ Incorrect
The parameter must be declared as '@autoclosure () -> Bool' to use autoclosure correctly.
4fill in blank
hardFill both blanks to declare a function with an autoclosure parameter and call it correctly.
Swift
func evaluate(_ expression: [1]) { print("Result is \(expression())") } evaluate([2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Int' instead of a closure type for the parameter.
Passing a value without an expression.
✗ Incorrect
The parameter must be '@autoclosure () -> Int' and the call passes an expression like '5 * 3' which is wrapped automatically.
5fill in blank
hardFill all three blanks to create a function with an autoclosure and escaping parameter, then call it.
Swift
func storeCondition(_ condition: [1], completion: @escaping () -> Void) { if condition() { completion() } } let conditionClosure: [2] = [3] > 10
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Bool' instead of closure types.
Assigning a raw value instead of an expression to the closure variable.
✗ Incorrect
The function parameter uses '@autoclosure () -> Bool', the variable is a closure '() -> Bool', and the expression '20 > 10' is assigned to it.