"if" statement not executing itself

Hello, I have a code here that, each time I run it, only the if statement which states "All fields are required" works but NOT ONLY when it must be called, it actually runs in place of the others. So whatever I do even when all the fields are complete, I have "All fields are required" as an alert message.
Here is the code, all help is appreciated, thank you in advance.

import UIKit

class RegisterPageViewController: UIViewController {


@IBOutlet weak var userEmailTextField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!
@IBOutlet weak var repeatPasswordTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func registerButtonTapped(_ sender: AnyObject) {
   
    let userEmail = ""
    let userPassword = ""
    let userRepeatPassword = ""

    
    
    // Check for empty fields
    
    
    if (userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty)
    {
        // Display Alert Message
        displayMyAlertMessage(userMessage:"All fields are required")
        return
    }

    //Check if passwords match
    if (userPassword != userRepeatPassword)
    {
        // Display an alert message
        displayMyAlertMessage(userMessage:"Passwords do not match")
        return
    }
    
    
    // Store data
    UserDefaults.standard.set(userEmail, forKey:"userEmail")
    UserDefaults.standard.set(userEmail, forKey:"userPassword")
    UserDefaults.standard.synchronize()
    
    
     // Display alert message with confirmation
    _ = UIAlertController(title:"Alert", message:"Registration is successfull. Thank you!", 
 preferredStyle:UIAlertControllerStyle.alert);
    
    _ = UIAlertAction(title:"Ok", style:UIAlertActionStyle.default)
    {
        action in
        self.dismiss(animated: true, completion:nil)
    }
    
    
    
   }


func displayMyAlertMessage(userMessage:String)
{

  let myAlert = UIAlertController(title:"Alert", message: userMessage, 
preferredStyle:UIAlertControllerStyle.alert);


 let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.default, handler:nil)

 myAlert.addAction(okAction)

 self.present(myAlert, animated:true, completion:nil)

    
}


}

If you’re following normal Cocoa Touch conventions then the code you posted in wrapped in a class. Something like this:

class MySomethingController : … {

    …

    @IBAction func registerButtonTapped(_ sender: AnyObject) {

        let userEmail = ""
        …
    }

    …
}

In that case I think the problem is that you’re declaring your values, like userEmail, in the wrong place. As shown above these values are local to the registerButtonTapped(_:) method, and I suspect you want them to be properties of the MySomethingController class. They’re also let and not var, so they can’t possibly change, and thus are always guaranteed to be empty.

So, you might want something like this:

class MySomethingController : … {

    var userEmail = ""

    …

    @IBAction func registerButtonTapped(_ sender: AnyObject) {
        …
    }

    …
}

That way other methods within the MySomethingController class can change the userEmail property, and thus it won’t always appear empty.

Alternatively you might want a structure like this:

class MySomethingController : … {

    @IBOutlet var userEmailField: UITextField!

    …

    @IBAction func registerButtonTapped(_ sender: AnyObject) {
        let userEmail = userEmailField.text ?? ""
        …
    }

    …
}

which extracts the text value directly from a UITextField that you’ve connected up in Xcode’s interface editor.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple