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

nil checking

Force unwrapping an optional could crash our applications. To eliminate the crashing problem, we can check whether the variable is not nil before unwrapping it.

The following example presents a simple nil checking approach:

if optionalString != nil {     print(optionalString!) } 

This approach is safe in compile and runtime but may cause problems during editing. For instance, if we accidentally move the print line outside the if block, the compiler is not going to complain and it may crash our application 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