swiftoption-type

How do you unwrap Swift optionals?


How do you properly unwrap both normal and implicit optionals?

There seems to be confusion in this topic and I would just like to have a reference for all of the ways and how they are useful.

There are currently two ways to create optionals:

var optionalString: String?

var implicitOptionalString: String!

What are all the ways to unwrap both? Also, what is the difference between using ! and ? during the unwrapping?


Solution

  • There are many similarities and just a handful of differences.

    (Regular) Optionals

    Implicitly Unwrapped Optionals