我对swift非常新,似乎无法通过此错误:
/Users/me/code/Apps/Aero-Eco/Aero-Eco/Controller/ViewController.swift:36:109: 无法将类型'()'的值转换为预期的参数类型 '((UIAlertAction) - > Void)?'
这是触发它的代码
@IBAction func loginPressed(_ sender: Any) {
Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
if error != nil {
print(error!)
let alert = UIAlertController(title: "Error", message: "Those credentials are not recognized.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: self.clearLogin()))
self.present(alert, animated: true, completion: nil)
} else {
print("Log in successful!")
self.clearLogin()
self.performSegue(withIdentifier: "goToSwitchboard", sender: self)
}
}
}
func clearLogin() {
self.emailTextField.text! = ""
self.passwordTextField.text! = ""
}
答案 0 :(得分:1)
这就是原因。您必须添加(action) in
let okAction = UIAlertAction(title: "OK".localized, style: .default, handler: {
(action) in//added this line
self.clearLogin()
})
答案 1 :(得分:0)
试试这个
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:{(action) in self.clearLogin()}))