Nico Auditore
Nico Auditore

Reputation: 21

expected declaration after if statement

I have an "expected declaration" at this line:

if(userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty)

Could you tell me why? Thank you in advance.

My code:

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 = userEmailTextField.text;
    let userPassword = userPasswordTextField.text;
    let userRepeatPassword  = repeatPasswordTextField.text
}

// Check for empty fields

if(userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty)
{

// Display Alert Message
displayMyAlertMessage("All fields are required");
return;

}

Upvotes: 1

Views: 100

Answers (3)

Nico Auditore
Nico Auditore

Reputation: 21

I put the if statement inside a function and it worked. Thank you for all your answers guys. Like that:

func registerButtonTapped() {  
    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
    }

Upvotes: 0

Rakesha Shastri
Rakesha Shastri

Reputation: 11242

Declare the variables in the class or even globally, not inside the button!

var userEmail: String = ""
var userPassword: String = ""
var userRepeatPassword: String = ""

Button declaration:

@IBAction func registerButtonTapped(_ sender: AnyObject) {
    userEmail = userEmailTextField.text;
    userPassword = userPasswordTextField.text;
    userRepeatPassword  = repeatPasswordTextField.text
}

Upvotes: 2

Mahesh Dangar
Mahesh Dangar

Reputation: 804

if(userEmail.text == "" || userPassword.text == "" || userRepeatPassword.text == "")
 {

      // Display Alert Message
      displayMyAlertMessage("All fields are required");
      return;

 }

or you can try

if(userEmail.text.isEmpty || userPassword.text.isEmpty || userRepeatPassword.text.isEmpty)
 {

      // Display Alert Message
      displayMyAlertMessage("All fields are required");
      return;

 }

try to check blank string and it will work the same

Upvotes: 1

Related Questions