Search code examples
iosswiftparse-platformpfquerypfobject

PFObject not working correctly with matching TextField String (Parse, Swift, iOS, Xcode 7 Beta)


My main goal is to have a user input a zipcode in the TextField, then parse will check the database too see if that zipcode matches one in the database. If it does, it'll allow the user to register and send out the println("youre in."). If not it'll print ("you're out")

This is my current code, and what my Parse backend looks like.

class checkAvailability: UIViewController, UITextFieldDelegate {

@IBOutlet weak var zipCode: UITextField!
@IBAction func checkAvailBtn(sender: AnyObject) {
    checkZip()
}

func checkZip() {
    let usersZipCode = zipCode.text
    let query = PFQuery(className:"zipCodes")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            // The find succeeded.
            print("Successfully retrieved \(objects!.count) zip codes.", terminator: "")
            // Do something with the found objects
            if let zipCodes = objects! as? [PFObject] {
                if zipCodes.contains({ $0["zipCodes"] as? String == usersZipCode }) {
                    print("your in!") // transition to the new screen

                }
                else {
                    print("your out.") // do whatever
                }
            }
        } else {
            // Log details of the failure
            print("Error: \(error!) \(error!.userInfo)", terminator: "")
        }
    }
}





override func viewDidLoad() {
    super.viewDidLoad()
    self.zipCode.delegate=self;
}
    //Allow only numbers
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        return Int(string) != nil
    }



//Display Navbar
override func viewWillAppear(animated: Bool)
{
    self.navigationController?.navigationBarHidden = false
}

 }

Here's a image of my database: enter image description here


Solution

  • I figured it out, each entry in my database had a ";" after the zip code. This is why the input was read incorrectly and not working.