Initializer for conditional binding must have Optional type, not CustomClass in Swift 5

I have just converted my project from swift 4 to swift 5 and received the following error.

I am just wondering why I did not receive this error in swift 4, but swift 5.

It seems that customErrorData can be nil, but why next if condition thinks that it is not optional type. I am confused.

Initializer for conditional binding must have Optional type, not 'CustomError'

if let customErrorData = try? decoder.decode(CustomError.self, from: data) as CustomError? {
  // error happens in the following line
  if let customError = customErrorData {
    print("custom error message")
  }
} else {
   print("generic error message")
}


Read more here: https://stackoverflow.com/questions/64961547/initializer-for-conditional-binding-must-have-optional-type-not-customclass-in

Content Attribution

This content was originally published by casillas at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: