1
\$\begingroup\$

I wrote this script and it's working quite well however I think there's too much if else statements on it. How could I change them for a switch statement?

@IBAction func sendLoginParameters(_ sender: UIButton) {
 if correoTextField.text?.isEmpty == true && contrasenaTextField.text?.isEmpty == true {
 let alertMessage = UIAlertController(title: "Datos requeridos", message: "Favor de llenar todos los datos requeridos (CORREO Y CONTRASEÑA) antes de continuar", preferredStyle: .alert)
 alertMessage.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: nil))
 self.present(alertMessage, animated: true, completion: nil)
 return
 } else if correoTextField.text?.isEmpty == true {
 let alertMessage = UIAlertController(title: "Datos requeridos", message: "Favor de llenar todos los datos requeridos (CORREO) antes de continuar", preferredStyle: .alert)
 alertMessage.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: nil))
 self.present(alertMessage, animated: true, completion: nil)
 return
 } else if contrasenaTextField.text?.isEmpty == true {
 let alertMessage = UIAlertController(title: "Datos requeridos", message: "Favor de llenar todos los datos requeridos (CONTRASEÑA) antes de continuar", preferredStyle: .alert)
 alertMessage.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: nil))
 self.present(alertMessage, animated: true, completion: nil)
 return
} else {
...
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Nov 17, 2016 at 2:46
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

In my opinion the issue here is not the repetition of if else but the repetition of similar alert message code.

For example, you could continue using if else but without these repetitions:

guard let correoText = correoTextField.text, let contrasenaText = contrasenaTextField.text else {
 fatalError("The outlets are not properly connected")
}
var msg: String? = nil
if correoText.isEmpty && contrasenaText.isEmpty {
 msg = "CORREO Y CONTRASEÑA"
} else if correoText.isEmpty {
 msg = "CORREO"
} else if contrasenaText.isEmpty {
 msg = "CONTRASEÑA"
}
if let msg = msg {
 let alertMessage = UIAlertController(title: "Datos requeridos", message: "Favor de llenar todos los datos requeridos (\(msg)) antes de continuar", preferredStyle: .alert)
 alertMessage.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: nil))
 self.present(alertMessage, animated: true, completion: nil)
}

But if you want to use switch, you can switch on a tuple representing the two booleans:

guard let correoText = correoTextField.text, let contrasenaText = contrasenaTextField.text else {
 fatalError("The outlets are not properly connected")
}
let msg: String?
switch (correoText.isEmpty, contrasenaText.isEmpty) {
case (true, true):
 msg = "CORREO Y CONTRASEÑA"
case (true, false):
 msg = "CORREO"
case (false, true):
 msg = "CONTRASEÑA"
default:
 msg = nil
 // implement additional behavior, or add more cases
}
let alertMessage = UIAlertController(title: "Datos requeridos", message: "Favor de llenar todos los datos requeridos (\(msg)) antes de continuar", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil)
answered Nov 17, 2016 at 12:46
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Nice improvements, especially the switch on tuple. However, the code presented will always display an alert message, even if the user has filled all required fields. Add an extra check to ensure msg has a value before displaying the alert. \$\endgroup\$ Commented Dec 1, 2016 at 15:51
0
\$\begingroup\$

A better solution may be to just disable the login button until all fields are filled.

However if you want to stick to this alert the error approach then move this logic out of the VC and into your login/api class.

Now the VC doesn't need to check the inputs at all, it just sends what it has to the login class and if any error occurs then it presents the alert with the error message.

More on how to do that here: https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html

answered Jun 5, 2018 at 7:56
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.