The Defer in Swift 5.0
May 24, 2021
Swift’s defer
keyword will help you to do some work after finishing the current scope.
The defer
statement syntax is here:
defer {
// statements
}
The statement inside the Defer will be called no matter whatever happened in the current scope. If you’ve used other programming languages, defer
will seem similar to try/finally
. Any code you defer will run no matter what, even if you throw an exception.