我正在查看Parse文档,如果提供的用户名和电子邮件是否已注册(使用),我希望使用这个有趣的函数来验证注册用户。该文档提供了以下示例:
var lotsOfWins = PFQuery(className:"Player")
lotsOfWins.whereKey("wins", greaterThan:150)
var fewWins = PFQuery(className:"Player")
fewWins.whereKey("wins", lessThan:5)
var query = PFQuery.orQueryWithSubqueries([lotsOfWins, fewWins])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains players with lots of wins or only a few wins.
}
}
我的版本是:
/*Check username and email not taken*/
let usernameInputCheck = PFQuery(className:"_User")
usernameInputCheck.whereKey("appUsername", equalTo: self.usernameTxtField.text!)
let emailInputCheck = PFQuery(className:"_User")
emailInputCheck.whereKey("email", equalTo:self.emailTxtField.text!)
let query = PFQuery.orQueryWithSubqueries([usernameInputCheck, emailInputCheck])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains players with lots of wins or only a few wins.
}
}
我希望能够检查是否采用了电子邮件或用户名,如果是,则设置一个标志,以便我可以向用户设置消息,例如:“提供的电子邮件已经注册”或“用户名不可用,请尝试另一个“。
提前感谢您的帮助。
更新:以下是解析文档的链接:https://parse.com/docs/ios/guide#queries-compound-queries
答案 0 :(得分:2)
这里有一些Objective-c可以轻松适应Swift。
PFQuery * nameQuery = [PFQuery queryWithClassName:@"user"]
[nameQuery whereKey:"username" equalTo:username];
PFQuery * emailQuery = [PFQuery queryWithClassName:@"user"];
[emailQuery whereKey:"email" equalTo:email];
PFQuery * query = [PFQuery orQueryWithSubqueries:@[nameQuery, emailQuery]
[query countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
if(number > 0) {
//Email address OR username is already in use.
}
}];
答案 1 :(得分:0)
解决问题:
让usernameInputCheck = PFQuery(className:" _User") usernameInputCheck.whereKey(" appUsername",equalTo:self.usernameTxtField.text!)
let emailInputCheck = PFQuery(className:"_User")
emailInputCheck.whereKey("email", equalTo:self.emailTxtField.text!)
let query = PFQuery.orQueryWithSubqueries([usernameInputCheck, emailInputCheck])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
let checkResult = self.emailOrUsernameIsTaken(results!)
if(checkResult != 0 && checkResult == 1)
{
//username taken
}else if (checkResult != 0 && checkResult == 2){
//email taken
}
}
}
这是函数:
func emailOrUsernameIsTaken(results: [PFObject])->Int
{
/*Check if username is taken or if email is taken*/
var preferenceTaken: Int = 0
if(results[0]["email"] as! String != "" && results[0]["email"] as! String == self.userObject.email!)
{
preferenceTaken = 1
}else if(results[0]["appUsername"] as! String != "" && results[0]["appUsername"] as! String == self.userObject.username!){
preferenceTaken = 2
}
return preferenceTaken
}