#19: Defer
Question
// Swift Quiz
— objc.io (@objcio) June 26, 2019
var counter = 5
func increment() -> Int {
defer { counter += 1 }
return counter
}
counter = increment()
// What's the value of counter?
Solution
Solution: 5
— objc.io (@objcio) June 27, 2019
The defer statement gets executed after the return statement, but before the function actually returns.
Therefore, the return value of 5 will be assigned to counter.https://t.co/s7500CWmAg