vendredi 8 avril 2016

Simplify multiple if fields validations in swift

i'm wondering if there's a cleaner way to handle this sign up validation, especially the last section where there're three if blocks. For completeness i've to say that every alert has a different message to show.

func fieldsAreValid() -> Bool {
  guard let email = (view.viewWithTag(0) as? UITextField)?.text,
    let password = (view.viewWithTag(1) as? UITextField)?.text else {
      //  show an alert
      return false
  }

  if !email.isEmail {
    // show an alert
    return false
  }

  if !password.isAStrongPassword {
    // show an alert
    return false
  }

  if isASignUp() {
    if let verification = (view.viewWithTag(2) as? UITextField)?.text {
      if verification != password {
        // show an alert
        return false
      }
    } else {
      // show an alert
      return false
    }
  }

  return true
}

Aucun commentaire:

Enregistrer un commentaire