如何使这个功能工作Swift

时间:2016-05-26 00:33:30

标签: swift swift2

我有2个功能

func checkEmailField() -> Bool {

    if(!loginPasswordTextField.text!.isEmpty){
        return true
    } else {
        print("Empty Field was found")
       // self.displayMessage(error.localizedDescription, fromViewController: self)
        return false

    }

}

func displayMessage(errorMessage:String?, fromViewController:UIViewController){

    let titleMessage = "Error"
    let alert = UIAlertController(title: titleMessage, message: errorMessage, preferredStyle: .Alert)
    let actionOK = UIAlertAction(title: "Ok", style: .Default, handler: nil)
    alert.addAction(actionOK)
    fromViewController.presentViewController(alert, animated: true, completion: nil)

}

当我在第一个func()中取消注释该行时,我得到一个错误未解析的标识符错误...我如何通过错误以便它可以工作?

1 个答案:

答案 0 :(得分:1)

因为您没有名为error的变量。你displayMessage函数需要一个字符串,所以只需传递一个字符串:

func checkEmailField() -> Bool {
    if(!loginPasswordTextField.text!.isEmpty){
        return true
    } else {
        print("Empty Field was found")
        self.displayMessage("Empty field was found", fromViewController: self)
        return false
    }
}