Member-only story

The nil coalescing operator in Swift

Let us focus on this way to avoid Force Unwrapping

Steven Curtis
4 min readJan 7, 2020

There are some techniques that can help the diligent programmer avoid the severe purgatory of force unwrapping. We focus on the nil coalescing operator because of it’s awesomeness and brevity. Let us get ready and…go!

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)

Terminology

Force unwrapping: Assume that an optional contains a value. Doing this results in a crash if the optional happens to be nil when this is attempted and therefore force unwrapping should only be attempted with the upmost caution.

Optional: Swift introduced optionals that handle the absence of a value, simply by declaring if there is a value or not. An optional is a type on it’s own!

Runtime: The period of time where a program is running

Avoiding Force Unwrapping Optionals

Optionals by definition are either a value, or nil.

Setting up variables

--

--

No responses yet