Autoclosures let you delay running some code until it is really needed, without writing extra code yourself.
Autoclosures (@autoclosure) in Swift
func functionName(parameter: @autoclosure () -> ReturnType) { // use parameter() to run the code }
The parameter marked with @autoclosure automatically wraps an expression into a closure.
You call the parameter like a function (parameter()) to run the wrapped code.
func printIfTrue(_ condition: @autoclosure () -> Bool) { if condition() { print("It's true!") } }
2 > 1 is automatically wrapped into a closure and passed.printIfTrue(2 > 1)
func logMessage(_ message: @autoclosure () -> String) { print("Log: \(message())") }
This program defines a function that takes a condition as an autoclosure. It prints a message before checking the condition, then prints if it is true or false. The expressions 3 > 2 and 1 > 5 are passed without manually writing closures.
func checkAndPrint(_ condition: @autoclosure () -> Bool) { print("Checking condition...") if condition() { print("Condition is true!") } else { print("Condition is false.") } } checkAndPrint(3 > 2) checkAndPrint(1 > 5)
Autoclosures help keep code clean and readable by hiding closure syntax.
Use @autoclosure only for simple expressions, not complex logic.
Remember to call the autoclosure parameter with parentheses to run the code.
Autoclosures wrap expressions automatically into closures.
They delay running code until you call the parameter like a function.
They make your code simpler and cleaner when passing conditions or messages.
Practice
@autoclosure attribute do in Swift?Solution
Step 1: Understand the purpose of @autoclosure
@autoclosure automatically wraps an expression passed as an argument into a closure, delaying its execution until called.Step 2: Compare with other options
Options B, C, and D describe behaviors unrelated to @autoclosure's purpose.Final Answer:
Automatically wraps an expression into a closure to delay its execution -> Option CQuick Check:
@autoclosure delays execution = A [OK]
- Thinking @autoclosure runs code immediately
- Confusing @autoclosure with normal closures
- Assuming it changes closure syntax
@autoclosure in Swift?Solution
Step 1: Recall correct syntax for @autoclosure
The correct syntax places @autoclosure before the closure type:@autoclosure () -> Bool.Step 2: Analyze each option
func check(_ condition: @autoclosure () -> Bool) { } matches the correct syntax. Options A, B, and C misuse the attribute placement or type declaration.Final Answer:
func check(_ condition: @autoclosure () -> Bool) { } -> Option AQuick Check:
@autoclosure syntax = func check(_ condition: @autoclosure () -> Bool) { } [OK]
- Placing @autoclosure after the type
- Using @autoclosure on non-closure types
- Omitting the closure arrow syntax
func logIfTrue(_ predicate: @autoclosure () -> Bool) {
if predicate() {
print("True")
} else {
print("False")
}
}
logIfTrue(3 > 5)Solution
Step 1: Understand the function behavior
The function takes a condition as an autoclosure and calls it inside the if statement.Step 2: Evaluate the passed expression
The expression3 > 5is false, sopredicate()returns false, triggering the else branch.Final Answer:
False -> Option DQuick Check:
3 > 5 is false, so output = False [OK]
- Assuming autoclosure runs immediately before function call
- Confusing true/false logic
- Expecting compile errors due to autoclosure
@autoclosure:func execute(_ action: @autoclosure () -> Void) {
action
}
execute(print("Hello"))Solution
Step 1: Check how the closure is called inside the function
The parameteractionis a closure and must be called with parentheses:action().Step 2: Identify the error
The code usesactionwithout parentheses, so the closure is not executed, causing no output.Final Answer:
Missing parentheses to call the closure inside the function -> Option AQuick Check:
Closure must be called with () = Missing parentheses to call the closure inside the function [OK]
- Forgetting to call the closure with parentheses
- Thinking @autoclosure forbids Void closures
- Assuming print can't be autoclosure
@autoclosure to log messages only when needed:func log(_ message: @autoclosure () -> String, onlyIf condition: Bool) {
if condition {
print(message())
}
}
log("Debug info", onlyIf: false)What is the main benefit of using
@autoclosure here?Solution
Step 1: Understand how @autoclosure delays evaluation
The message string expression is wrapped in a closure and only evaluated whenmessage()is called.Step 2: Analyze the condition effect
Sinceconditionis false,message()is never called, so the string is not constructed, saving computation.Final Answer:
The message string is constructed only if the condition is true, saving resources -> Option BQuick Check:
@autoclosure delays message creation = The message string is constructed only if the condition is true, saving resources [OK]
- Thinking message is always created regardless of condition
- Assuming autoclosure prints automatically
- Confusing condition with message parameter
Ready to prove it?
Pass the test and this pattern moves unknown
