【发布时间】:2016-04-11 16:06:25
【问题描述】:
如何将错误显示为正常消息:
我有一个登录功能:
func signIn() {
PFUser.logInWithUsernameInBackground(self.usernameTextField.text!, password: self.passwordTextField.text!) {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
// Do stuff after successful login.
print("User successfully logged in: \(user)")
self.performSegueWithIdentifier("loginSegue", sender: nil)
} else {
// The login failed. Check error to see why.
print("Server reported an error: \(error)")
// create the alert
let alert = UIAlertController(title: "Error", message: "\(error)", preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// show the alert
self.presentViewController(alert, animated: true, completion: nil)
}
}
}
UIAlertController 会向用户显示:
但我怎样才能只显示消息:
用户名/密码无效。
我尝试使用 error.message,但这不是命令,并且 error.description 也不起作用。有什么建议吗?
【问题讨论】:
-
在这种情况下只显示
localisedDescription.... -
我还没有阅读答案...我阅读了问题。