Swift - Implicitly Unwrapped Optionals


To access the value of an optional variable without using the ! character, you can declare an optional type as an implicitly unwrapped optional.

Consider the following declaration:


//implicit optional variable
var str2: String? = "This is a string"
print(str2) // "This is a string"
str2 = nil/*  w w w .j a  v  a2 s  . c  o  m*/
print(str2) // nil


Here, str2 is an implicitly unwrapped optional.

When you access str2 , there is no need to use the ! character, as it is implicitly unwrapped:

If str2 is set to nil , accessing the str2 will return a nil :

Related Topic