在用户键入iOS时检查用户名可用性

时间:2015-10-16 05:59:07

标签: ios swift parse-platform

就像标题说得好,我想在用户输入时检查用户是否存在于parse.com中。到目前为止,我从他们的文档中得到了这段代码,但是如果你有更好的建议

let userName = userNameTextField.text
let query = PFUser.query()
query!.whereKey("Username", equalTo:"%userName%")
let usernames = query!.findObjects()
if usernames != nil { print("existing")} else {print("notexisting")}

1 个答案:

答案 0 :(得分:0)

简单的例子:

// First get user's inputted text
let enteredUserName = userNameTextField.text

// Then query and compare
var query = PFQuery(className: "User")
query.whereKey("yourUsers", equalTo: enteredUserName)
query.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]?, error: NSError?) in
    if error == nil {
        if (objects!.count > 0){
            isTaken = true
            println("username is taken")
        } else {
            println("Username is available. ")
        }
    } else {
        println("error")
    }
}

来自THIS帖子。