This question can be summed up to "When should additional options be used?". There are many great articles and documentation on this issue, but I will try to combine my experience with it, as well as the documentation and articles that I read.
While options have very specific functionality when used, I would like to think of them more as a way of saying something about the variable itself, rather than declaring functionality. When I read:
var myVar:Class? = nil
This means that we should never anticipate that myVar assigned, and instead we should always expect both conditions, the first of which is that myVar matters, but it doesn’t. I assume that these things are related to the functionality that adds ? to the table. The compiler will not allow you to use myVar without unpacking it. Because of this, the compiler suggests (with every access to a property or function) to use this syntax:
myVar?.myProperty = something
Because of ? before . this line of code will check to see if myVar nil before myVar and executing the line of code. Thus, we expected and processed both conditions. This line of code will essentially be “ignored” if myVar is zero and executed if it is not.
This is different from another type of optional ! :
myVar!.myProperty = something
This will always try to unpack myVar . This line of code throws an exception saying that: "Suddenly, zero was found while expanding the value." Bye ? will fail.
If we change myVar declaration to use ! optional:
var myVar:Class! = nil
Then we can always use myVar without receiving a compiler error, saying that we need to deploy myVar before using it. For example, unlike another optional ( ? ), We can say:
myVar.myProperty = something
This line is equivalent to:
myVar!.myProperty = something
So, if myVar is nil , then we crash the program.
Output:
Using any of these options (or simply not using optional at all), we will inform the user about myVar things about myVar because of how the language forces you to or not to force you to deal with myVar .
? optional var myVar:Class? = nil var myVar:Class? = nil :
Should I use the option ? optional, we essentially force the user to always check nil .
! optional var myVar:Class! = nil var myVar:Class! = nil :
If we use ! , then if myVar is nil, something is wrong and we need to minimize the program, however, the user still has the ability to handle the nil case, which is especially useful if the user who was to assign myVar . The big advantage of this is network requests.
no optional var myVar = Class() :
Do not use optional with all means (obviously) that the variable is always , and we do not need to worry about that it is nil .