Swift - Implicitly Unwrapped Optionals

Introduction

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:

Demo

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

Result

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