2016-10-13 30 views
0

我會開始用Swift和Parse編寫註冊視圖控制器嗎? 標誌了有檢查,如果用戶名或電子郵件都採取了在解析客戶端在swift上處理PFErrorCode

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()相同我想要的。

回答

0

這是因爲您沒有比較錯誤的代碼,而是使用不同的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") 
} 

此外,電子郵件是在解析獨特的,所以你應該考慮使用getFirstObjectInBackground而不是findObjectsInBackground。並使用PFUser.query()而不是_PFQuery(className:「User」)

+0

嘿nts,我複製了擴展裏面的不活動按鈕,並在編譯器中彈出一些錯誤。 – user2508528

+0

將擴展名粘貼到另一個文件中(例如:PFErrorCode + Equatable) – nathan

+0

Hey nts,我嘗試了您寫的擴展名,並收到三條錯誤消息。你能更具體地把擴展名放在哪裏嗎? – user2508528