What is the preferred approach to check and unwrap an optional Error

This shouldn't be preferred at all in this situation. Furthermore you ignore that error is still optional and the compiler will emit a warning when you pass an optional value to print. (Or have you missed the force unwrap? But I still discourage you from writing code like this even though it's true that you're safe to force unwrap there.)

I think the cleanest solution here is to unwrap it and return from the main function or proceed otherwise.

func f2(error: Error?) {
  if let error = error {
    return print("Error: \(error)")
  }
  print("start processing")
}

So f2 is the way to go here. I also would consider f3 as good enough but that depends on the nested scopes that will follow in the real wold use case (avoid pyramid of doom).

1 Like