Optionals

May 29, 2019 ยท View on GitHub

When do we use them?

Optionals are used when something is not known at the time of initialization, when an API can fail, or when the absence of a value provides additional meaning.

Key Considerations

  • Evaluate if you need an optional value, and avoid them if possible.
  • Do not make Bools optional.
    • A tri-state Bool can be represented in a more structured way, such as an enum with three well-named cases.
  • Avoid making Arrays optional.
    • Only do this if it provides meaning beyond it just being empty.
  • System APIs may require us to use optional values since they return optional values.
    • URLs created using init?(string:) are a common example.
  • weak properties must be optional by definition because they can become nil.

Dealing with Optionals

  • Always handle unwrapping optionals safely.
  • We prefer conditional binding (if let) over flatMap.
  • If a function requires an optional value to have a value, we opt to bind with guard statements and return early.

Reference Docs