Optional types
In our day-to-day application development, we encounter situations where we expect to receive a value but we do not receive it. For instance, suppose that we have a list of items and we need to search for a particular value in the list. The particular value that we are looking for might not be on the list. Other examples can be calling a web service and receiving a JSON payload without the fields that we are looking for, or querying a database and not receiving the expected values.
What are we going to receive when the value is not there, and how will we handle this absence?
In programming languages such as C, it is possible to create a variable without giving it a value. If we try to use the variable before assigning a value, we would get an undefined value.
In Swift, we can define a variable without giving it a value, but we cannot use it without assigning some value to it. In other words, we need to initialize it before being able to use it. This feature of Swift ensures that...