swift - 快速处理 PFErrorCode

标签 swift parse-platform

我可以开始用 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/

相关文章:

ios - 在按钮外开始触摸并在拖入按钮边界时触发事件

ios - 键盘显示时 Tableview 滚动内容

ios - 如何使 PFQueryTableView 在 UIViewController 中工作

swift - 如何根据 'location changes' 不断更新 tableview?

ios - 分配一个枚举器,它与变量相反

ios - Swift 使用 CollectionType .filter .indexOf 和 .map 在元组数组中查找值

swift - 无法将类型 'MenuView' 的值分配给类型 'some View'

Android 解析 "invalid session token"错误

javascript - 使用 Javascript 从 Parse 访问 JSON 数据

ios - Parse Push Notifications 可以针对特定的 iOS 吗?