if语句后的预期声明

时间:2018-08-04 04:21:56

标签: ios swift if-statement

我在这一行有一个“期望的声明”:

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

你能告诉我为什么吗?预先谢谢你。

我的代码:

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
}

//检查是否有空字段

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

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

}

3 个答案:

答案 0 :(得分:2)

在类甚至全局中声明变量,而不是在按钮内部!

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

按钮声明:

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

答案 1 :(得分:1)

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

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

 }

或者您可以尝试

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

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

 }

尝试检查空白字符串,它将起作用相同

答案 2 :(得分:0)

我将if语句放入一个函数中,它起作用了。 谢谢大家的回答。 像这样:

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
    }