Skip to content

Latest commit

 

History

History
21 lines (18 loc) · 1.13 KB

Optionals.md

File metadata and controls

21 lines (18 loc) · 1.13 KB

Optionals

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