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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset