0
0
Swiftprogramming~20 mins

Autoclosures (@autoclosure) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Autoclosure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code using @autoclosure?
Consider the following Swift code snippet. What will be printed when it runs?
Swift
func logIfTrue(_ predicate: @autoclosure () -> Bool) {
    if predicate() {
        print("It's true!")
    } else {
        print("It's false!")
    }
}

logIfTrue(2 > 1)
AIt's true!
BIt's false!
CCompilation error due to missing parentheses
DRuntime error
Attempts:
2 left
💡 Hint
Remember that @autoclosure delays evaluation until the function uses the predicate.
🧠 Conceptual
intermediate
1:30remaining
What is the main benefit of using @autoclosure in Swift?
Why would a Swift programmer use the @autoclosure attribute on a function parameter?
ATo force the expression to be evaluated multiple times
BTo convert a closure into a normal function
CTo delay the evaluation of an expression until it is used inside the function
DTo automatically evaluate the expression before passing it
Attempts:
2 left
💡 Hint
Think about when the expression inside the function is actually run.
🔧 Debug
advanced
2:30remaining
Why does this Swift code cause a compile error?
Examine the code below. Why does it fail to compile?
Swift
func check(_ condition: @autoclosure () -> Bool) {
    if condition {
        print("True")
    } else {
        print("False")
    }
}

check(5 > 3)
ABecause the function call is missing parentheses
BBecause the condition parameter is missing the @escaping attribute
CBecause @autoclosure cannot be used with Bool return types
DBecause @autoclosure parameters must be called as functions inside the body
Attempts:
2 left
💡 Hint
How do you use a closure parameter inside a function?
📝 Syntax
advanced
2:00remaining
Which option correctly declares a function with an @autoclosure parameter that can escape?
Select the correct Swift function declaration that uses @autoclosure and allows the closure to escape the function body.
Afunc storeCondition(_ condition: @autoclosure () -> Bool) { /* ... */ }
Bfunc storeCondition(_ condition: @autoclosure @escaping () -> Bool) { /* ... */ }
Cfunc storeCondition(_ condition: @escaping @autoclosure () -> Bool) { /* ... */ }
Dfunc storeCondition(_ condition: () -> @autoclosure Bool) { /* ... */ }
Attempts:
2 left
💡 Hint
Remember the order of attributes matters in Swift.
🚀 Application
expert
2:30remaining
How many times is the expression evaluated in this Swift code?
Given the code below, how many times will the expression 'print("Evaluated") ; return true' be executed?
Swift
func test(_ condition: @autoclosure () -> Bool) {
    if condition() {
        print("First check passed")
    }
    if condition() {
        print("Second check passed")
    }
}

test(print("Evaluated"); return true)
ATwice
BZero times
COnce
DThree times
Attempts:
2 left
💡 Hint
Think about when the closure is called and how many times.