Doing it later with defer
Typically, when we call a function, control passes from the call site to the function, and then the statements within the function are executed sequentially until either the end of the function or until a return statement, as shown in the following diagram:

Sometimes, it can be useful to execute some code after the function has returned, but before control has been returned to the call site. This is where Swift's defer statement can be useful:

Getting ready
A defer statement can be useful to change state once a function's execution is complete, or to clean up values that are no longer needed. Let's look at an example of updating state with a defer statement.
Imagine that we have movie reviews with star ratings, and we want to classify them based on their star rating.
How to do it...
Let's get started:
- First, let's define the options that a movie review may be classified into:
enum MovieReviewClass { case bad case average case good case brilliant }
- Let...