Challenge - 5 Problems
Swift Closure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a closure capturing a variable
What will be printed when this Swift code runs?
iOS Swift
func makeIncrementer(amount: Int) -> () -> Int {
var total = 0
let incrementer: () -> Int = {
total += amount
return total
}
return incrementer
}
let incrementByTen = makeIncrementer(amount: 10)
print(incrementByTen())
print(incrementByTen())Attempts:
2 left
💡 Hint
Closures can capture and modify variables from their surrounding context.
✗ Incorrect
The closure captures the variable 'total' and adds 'amount' each time it is called. The first call adds 10 to 0, returning 10. The second call adds 10 to 10, returning 20.
❓ ui_behavior
intermediate1:30remaining
Closure as a button action handler
In SwiftUI, what happens when this button is tapped?
iOS Swift
struct ContentView: View {
@State private var count = 0
var body: some View {
Button(action: {
count += 1
}) {
Text("Tap count: \(count)")
}
}
}Attempts:
2 left
💡 Hint
State variables update the UI when changed inside closures.
✗ Incorrect
The closure increments the @State variable 'count' on each tap, causing the view to update and show the new count.
❓ lifecycle
advanced2:30remaining
Memory management with closures
What problem can occur with this Swift code involving closures?
iOS Swift
class ViewController { var completionHandler: (() -> Void)? func setup() { completionHandler = { print(self) } } }
Attempts:
2 left
💡 Hint
Closures capture variables strongly unless specified otherwise.
✗ Incorrect
The closure captures 'self' strongly, and 'self' holds the closure, creating a retain cycle that prevents deallocation.
📝 Syntax
advanced1:30remaining
Correct syntax for a closure with parameters and return value
Which option correctly defines a closure that takes two Ints and returns their sum?
Attempts:
2 left
💡 Hint
Closures with parameters and return types need 'in' keyword separating parameters and body.
✗ Incorrect
Option B uses the full closure syntax with parameter types, return type, and 'in' keyword correctly. Option B is invalid syntax. Option B lacks parameter types and return type, so it won't compile. Option B lacks parameter types, causing a compile error.
🔧 Debug
expert3:00remaining
Why does this closure cause a crash?
This Swift code crashes at runtime. What is the cause?
iOS Swift
class Example { var closure: (() -> Void)? func configure() { closure = { [unowned self] in print(self.description) } } } var example: Example? = Example() example?.configure() let capturedClosure = example!.closure! example = nil capturedClosure()
Attempts:
2 left
💡 Hint
Unowned references do not keep the object alive and cause crash if accessed after deallocation.
✗ Incorrect
The closure captures 'self' as unowned. After 'example' is set to nil, 'self' is deallocated. Calling the closure tries to access a deallocated object, causing a runtime crash.