Swift 3: get error code

Hey community 🙂


I have a small problem, while I upgraded my App to Swift 3.


Previously I used the error code to evaluate a problem and display a user friendly answer:


FIRAuth.auth()?.signIn(withEmail: withEmail, password: password,
                               completion: {
                                user, error in

// some code

switch errorcode {
  case 17009:
      print(error)
      popupDialog("Falsche Eingabe", message: "Das Passwort oder die Email Adresse sind falsch. Bitte ändere die Daten und versuche es erneut", button: "Okay")
  case 17011:
      print(error)
      popupDialog("Benutzer nicht vorhanden", message: "Dieser Benutzer existiert nicht! Bitte kontrolliere die Eingabe oder erstelle ein neues Konto", button: "Okay")
  default:
      print(error)
      popupDialog("Error", message: "Ein unbekannter Fehler ist aufgetreten. Bitte versuche es erneut", button: "Okay")
}


the problem now is, that I can't access the error code with the following statement:


let errorcode = error!.code



How can I save the error in a variable easily? Or how can I respond to specific errors?


I look forward to your answers 🙂

Accepted Answer

In Swift 3, all types conforming to `Error` protocol can safely bridged to `NSError`.


    let errorCode = (error as NSError).code

Okay very nice and thanks for your quick answer 🙂

Swift 3: get error code
 
 
Q