gpt4 book ai didi

ios - 注册时发生错误处理

转载 作者:行者123 更新时间:2023-11-30 12:16:32 24 4
gpt4 key购买 nike

当我尝试 else 语句时,代码会停止并给出此错误“使用未解析的标识符‘错误’”。我该如何修复它并仍然有 UIAlert?

        @IBAction func registerTapped(_ sender: Any) {
print("tapped")
let namec = nameTextField.text
if let email = emailTextField.text, let pass = passwordTextField.text, let name = (namec?.capitalized.isEmpty)! ? nil:namec?.capitalized {
FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in
if user != nil {
//user found


let interval = NSDate().timeIntervalSince1970
let date = Date(timeIntervalSince1970: interval)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy/HH/mm/SS"
// you can change the date format to whatever you wants
let dateString = dateFormatter.string(from: date)
print(dateString)
self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": dateString])
print("User Created And Added To Database", email, name, dateString)
self.performSegue(withIdentifier: "registertologin", sender: self)
}
})
} else {
print(error!)
let alert = UIAlertController(title: "Error Creating Account ", message: "\(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}

最佳答案

error 不在代码块内,在您的 else 语句中,error 变量不存在,代码应该看起来更像这样:

@IBAction func registerTapped(_ sender: Any) {
print("tapped")
let namec = nameTextField.text
if let email = emailTextField.text, let pass = passwordTextField.text, let name = (namec?.capitalized.isEmpty)! ? nil:namec?.capitalized {
FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in
if error != nil {
print(error!)
let alert = UIAlertController(title: "Error Creating Account ", message: "\(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
} else {
//Check if this is actually necessary, probbaly if user is nil that means your error variable is set to something, making this check unnecessary.
if user != nil {
//user found
let interval = NSDate().timeIntervalSince1970
let date = Date(timeIntervalSince1970: interval)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy/HH/mm/SS"
// you can change the date format to whatever you wants
let dateString = dateFormatter.string(from: date)
print(dateString)
self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": dateString])
print("User Created And Added To Database", email, name, dateString)
self.performSegue(withIdentifier: "registertologin", sender: self)
} else {
self.showAlertView(withTitle: "Error", andMessage: "User not found")
}
}
})
} else {
self.showAlertView(withTitle: "Missing input", andMessage: "Please make sure email and password fields are filled.")
}
}
}


func showAlertView(withTitle aTitle: String, andMessage aMessage: String) {
let alertController = UIAlertController(title: aTitle message: aMessage, preferredStyle: .alert)
let actionOk = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(actionOk)

//This part of code needs to run on the main thread since your alert might be
//executed form the registration code block, which might be on a background thread
DispatchQueue.main.async {
self.presentViewController(alertController, animated: true, completion: nil)
}
}

关于ios - 注册时发生错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45367721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com