O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Force unwrapping

To unwrap Optionals, the easiest and most dangerous method that we can use is force unwrapping. In short, ! can be used to force unwrap the value from Optional.

The following example forcefully unwraps optionalString:

optionalString = "An optional String" print(optionalString!) 

Force unwrapping the Optionals may cause errors if the optional does not have a value, so it is not recommended to use this approach as it is very hard to be sure if we are going to have values in Optionals in different circumstances.

In fact, force unwrapping eliminates the benefits of type safety and may cause our applications to crash during runtime.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required