1
我的主要目標是讓用戶在TextField中輸入一個zipcode,然後解析也會檢查數據庫,看看該zipcode是否匹配在數據庫中。如果是這樣,它將允許用戶註冊併發送println(「youre in」)。 如果不是,它會打印(「你出去」)PFObject無法正確匹配TextField字符串(解析,Swift,iOS,Xcode 7 Beta)
這是我當前的代碼,以及我的Parse後端的樣子。
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
}
}