Implicitly Unwrapped Optionals

Sometimes you want to create an optional that gets unwrapped automatically. To do this, you assign the type with an exclamation point instead of a question mark:

var hasSomething:String! = "Hey there"// implicitly unwrapped optional stringhasSomething // print the implicitly unwrapped optional and get the   unwrapped value.

You can think of implicitly unwrapped optionals as a present that unwraps itself. You should not use an implicitly unwrapped optional if a chance exists that it may contain nil at any point. You can still use implicitly unwrapped optionals in value binding to check their values.

So why should you create implicitly unwrapped optionals in the first place if they can ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.