RubberDucky4444 RubberDucky4444 - 6 months ago 10
Swift Question

How to get this func to work Swift

I have 2 funcs

func checkEmailField() -> Bool {

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

}

}


and

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)

}


When i uncomment out that line in the first func() i get an error unresolved identifier error ...how do i pass through error so it works?

Answer

Because you don't have a variable called error. You displayMessage function expects a string so simply pass it a string:

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
    }
}
Comments