lundi 6 juillet 2020

How to have code inside of a guard else statement in swift?

I have the following code:

@IBAction func loginTapped(_ sender: Any) {
    let error = validateFieldsSignIn()
    if error != nil {
        showErrorSignIn(error!)
    } else {
        guard let emailsignin = emailSignIn?.text!.trimmingCharacters(in: .whitespacesAndNewlines), 
            let passwordsignin = passwordSignIn?.text!.trimmingCharacters(in: .whitespacesAndNewlines) else  {
            return showErrorSignIn("Fill in all fields")
        }
        Auth.auth().signIn(withEmail: emailsignin, password: passwordsignin) { (user, error) in
            if error != nil {
                print("There was an error")
                self.errorLabel3.text = "Invalid username or password"
                self.errorLabel3.alpha = 1
            } else {
                self.transitionToHome()
            }
        }
    }
}

Although unless the fields aren't filled in the else statement gets triggered and the error label says fill in all fields, essential the code that is getting triggered is this:

else  {
    return showErrorSignIn("Fill in all fields")
}

I tried putting the Auth.auth().signIn() inside the else block although I got the following error:

Variable declared in 'guard' condition is not usable in its body

How do I fix this error message?

Aucun commentaire:

Enregistrer un commentaire