After I query the Parse database I get an error with the following code:
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["Zipcode"] as? Int32 == usersZipCode}) { **<-----THIS IS WHERE THE ERROR IS**
print("your in!") // transition to the new screen
self.performSegueWithIdentifier("beginSignUp", sender: self)
}
else {
self.messageLabelNotAvail.text = "Unfortunately, Patch is not available in your area or you have not typed in a correct US Zip Code."
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)", terminator: "")
}
}
}
If i replace Int32 to String, it works fine.. But my Zipcode in my parse database is a Number not a String. What am I doing wrong?
Instead of:
if zipCodes.contains({ $0["Zipcode"] as? Int32 == usersZipCode}) {
//Rest of Code
}
Try:
if let target = Int32(usersZipCode)
where zipCodes.contains({ $0["Zipcode"] as? Int32 == target}) {
//Rest of Code
}
Clarification: You can't compare things of different types in Swift. The reason it works when you cast to
String
but breaks when you cast toInt32
seems to be thatusersZipCode
is ofString
type.