1

Like the title explained well, I'm looking to check if user exist in parse.com while the user is typing. I came out so far with this code from their documentation but if you have better suggestion

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")}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136

1 Answers1

0

Simple example for you:

// 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")
    }
}

From THIS post.

Community
  • 1
  • 1
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • I add in your query istaken... but it's still not working I have always the same answer available even in parse the username is existing ... –  Oct 16 '15 at 06:30
  • I found it ! your have to update (className: "_User") by (className: "_User") ! but what does mean "_" ? –  Oct 16 '15 at 06:33
  • But how to show for the user if the user is taken while the user is typing how to add that in my code ? –  Oct 16 '15 at 06:37
  • You can use delegate methods of textField. – Dharmesh Kheni Oct 16 '15 at 06:38
  • Can you be more specific please with an example thanks ! –  Oct 16 '15 at 06:39
  • This will help: http://stackoverflow.com/questions/28394933/how-do-i-check-when-a-text-field-changes-in-swift – Dharmesh Kheni Oct 16 '15 at 06:40
  • so from what I understand I have to add this code ? but where ? textField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged) func textFieldDidChange(textField: UITextField) { //mycode } –  Oct 16 '15 at 06:50
  • Follow this tutorial: http://sourcefreeze.com/uitextfield-and-uitextfield-delegate-in-swift/ – Dharmesh Kheni Oct 16 '15 at 06:52
  • thanks ! but I'm stack how to populate the UILabel with my print() while i'm typing ? do you have any idea ? –  Oct 16 '15 at 07:39
  • Feel free to ask another question with more detail.. :) – Dharmesh Kheni Oct 16 '15 at 07:40
  • http://stackoverflow.com/questions/33165441/how-to-populate-my-uilabel-from-my-uitext-while-im-typing-with-spinning-icon@DharmeshKheni –  Oct 16 '15 at 08:18