好的,当我在iPhone上运行我的应用程序时,它允许我在文本字段中输入信息,但是当我点击“注册”时会崩溃。按钮。
这是视图控制器中的showAlert函数:
2016-12-12 20:24:37.943 Elixr [2013:473247] AWSiOSSDK v2.4.12 [Debug] AWSURLResponseSerialization.m line:63 | - [AWSJSONResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] |回应机构: {" __ type":" NotAuthorizedException"," message":"无法验证客户端5的秘密哈希值pmvgst5c9650q4v9dfii8g8h7"} 无法转换类型' NSError' (0x1a7abc0f0)到' NSString' (0x1a7abc398)。
2016-12-12 20:24:37.955718 Elixr [2013:472573]无法转换类型' NSError' (0x1a7abc0f0)到' NSString' (0x1a7abc398)。
这就是“注册”的IBAction'按钮:
@IBAction func SignUpButton(_ sender: Any) {
guard let usernameValue = self.Username.text, !usernameValue.isEmpty,
let passwordValue = self.Password.text, !passwordValue.isEmpty else {
showAlert(title: "Missing required fields", message: "Please enter a username or password.")
return
}
var attributes = [AWSCognitoIdentityUserAttributeType]()
if let emailValue = self.Email.text, !emailValue.isEmpty {
let email = AWSCognitoIdentityUserAttributeType()
email?.name = "email"
email?.value = emailValue
attributes.append(email!)
}
// Sign up the user
self.pool?.signUp(usernameValue, password: passwordValue, userAttributes: attributes, validationData: nil).continue ({[weak self] (task: AWSTask ) -> AnyObject? in
guard let strongSelf = self else { return nil }
DispatchQueue.main.async(execute: {
if let error = task.error {
self?.showAlert(title: "Error", message: error as! String) ---> ERROR OCCURS ON THIS LINE <---
} else if let result = task.result {
// handle the case where user has to confirm his identity via email / SMS
if (result.user.confirmedStatus != AWSCognitoIdentityUserStatus.confirmed) {
strongSelf.sentTo = result.codeDeliveryDetails?.destination
strongSelf.performSegue(withIdentifier: "SignUpConfirmSegue", sender:sender)
} else {
self?.showAlert(title: "Registration Complete", message: "Registration was successful.")
strongSelf.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
})
return nil
})
这是showAlert函数:
func showAlert(title : String, message : String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { _ in
}))
self.present(alert, animated: true, completion: nil)
}
答案 0 :(得分:3)
一般的好方法是在内部处理错误并仅向用户显示&#34;用户友好&#34;根据导致该错误的原因发出的消息(例如,我们很抱歉,但似乎...... /请再试一次... /输入您的密码才能登录/没有互联网/...)
Apple的文档说明了以下内容
仅当您确定向下转换将始终成功时,才使用类型转换运算符的强制形式(作为!)。如果您尝试向下转换为不正确的类类型,则此形式的运算符将触发运行时错误。
var localizedDescription :String包含本地化的字符串 错误的描述。
showAlert(title: "Error", message: error.localizedDescription)
答案 1 :(得分:1)
使用(NS)Error
的{{3}}属性显示错误消息。
self?.showAlert(title: "Error", message: error.localizedDescription)