Sentinel values
A valid value that represents a special condition such as the absence of a value is known as a sentinel value. That’s what your empty string would be in the previous example.
Let’s look at another example. Say your code requests something from a server, and you use a variable to store any returned error code:
var errorCode = 0
In the success case, you represent the lack of an error with a zero. That means 0 is a sentinel value.
Just like the empty string for occupation, this works, but it’s potentially confusing for the programmer. 0 might actually be a valid error code — or could be in the future, if the server changed how it responded. Either way, you can’t be completely confident that the server didn’t return an error.
In these two examples, it would be much better if there were a special type that could represent the absence of a value. It would then be explicit when a value exists and when one doesn’t.
Nil is the name given to the absence of a value, and you’re about to see how Swift incorporates this concept directly into the language in a rather elegant way.
Some other programming languages simply use sentinel values. Some, like Objective-C, have the concept of nil, but it is merely a synonym for zero. It is just another sentinel value.
Swift introduces a whole new type, optional, that handles the possibility a value could be nil. If you’re handling a non-optional type, then you’re guaranteed to have a value and don’t need to worry about the existence of a valid value. Similarly, if you are using an optional type then you know you must handle the nil case. It removes the ambiguity introduced by using sentinel values.