Optionals and Unwrapping optionals in swift

(map/flatmap/optional chaining/optional binding/ Optional casting and optional error handling)

What is optional?

How to create an Optional?

optionalString has a value or there isn’t a value.

Optional is a super powered enum which has two values. None and Some(Wrapped), where Wrapped is an associated value of the correct data type. It looks like this

In fact, the Optional enum uses the generic type placeholder Wrapped so that any type can be made optional!

What are the ways to unwrap optionals?

  1. Forced Unwrapping
  2. Optional Binding
  3. Optional chaining
  4. With implicitly unwrapped optionals, using !
  5. Nil coalescing operator
  6. Unwrapping using higher order function

Forced Unwrapping:- Unsafe.

Optional Binding:- Safe

Optional Chaining:- Safe

Sample example for optional chaining.

With implicitly unwrapped optionals, using ! :- Unsafe

Nil coalescing operator:- Safe

Unwrapping using higher order function:- Safe

How Optional is used for Error Handling?

What is Optional Casting?

How can we use switch statement with optional?

I am happy to share this article with you. If you’ve enjoyed this article, do show support by giving a few claps 👏 . Thanks for your time and make sure to follow me or drop your comment below 👇

iOS Developer in walmart