It's comforting to think that in the code you write, everything will always happen as expected, and your program can handle any eventuality. However, sometimes things can go wrong – really wrong. A situation could arise that you know is possible but don't expect to ever happen, and the program should terminate if it does. In this recipe, we will look at two issues like this: fatalError and precondition.
Getting ready
Let's reuse our example from the previous recipe; we have an object that can be used to classify movie reviews based on how many stars out of 10 the review gave the movie. However, let's simplify its use, and say that we only intend for a classifier object to classify one, and only one, movie review.
How to do it...
Let's set up our movie classifier to only be used once, and only accept ratings out of 10:
- Define the classification state and the movie review class:
enum ClassificationState {
...