我可以开始用 Swift 和 Parse 编写一个注册 View Controller 吗? 注册时会检查解析客户端是否采用了用户名或电子邮件
let query = PFQuery(className:"_User")
query.whereKey("email", equalTo: email)
query.findObjectsInBackground { (succeeded, error) -> Void in
newUser.signUpInBackground{(success, error) -> Void in
// The find succeeded.
print("Successfully retrieved scores.")
if success {
// Do something with the found objects
let alertMessage = UIAlertController(title: "Register complated", message: "You've been registered.", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)
alertMessage.addAction(okAction)
self.present(alertMessage, animated: true, completion: nil)
}
// Log details of the failure
// username is exists
else {
if PFErrorCode.errorUsernameTaken.rawValue == 202 {
print ("Username is exists")
}
else if PFErrorCode.errorUserEmailTaken.rawValue == 203 {
print ("E-mail is exists")
}
}
当我尝试写入现有的电子邮件或密码时,控制台中的输出没有显示与我想要的相同的 print () 。
最佳答案
这是因为您没有比较错误的代码,而是比较固定数字与不同的 PFErrorCodes。
/// Your code
if let error = error, error._code == PFErrorCode.errorUsernameTaken.rawValue {
print ("Username is exists")
} else if let error = error, error._code == PFErrorCode.errorUserEmailTaken.rawValue {
print ("E-mail is exists")
}
此外,电子邮件在 Parse 中是唯一的,因此您应该考虑使用 getFirstObjectInBackground 而不是 findObjectsInBackground。并使用 PFUser.query() 而不是 _PFQuery(className:"User")。
关于swift - 快速处理 PFErrorCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40028202/