I have a function called recognizeDropoff and it worked fine with Swift 1.2 in Xcode 6.4. However, now I'm using Xcode 7.1 with Swift 2 and I am getting this error: Call can throw, but it is not marked with 'try' and the error is not handled
func recognizeDropoff() {
let currentUsername = PFUser.currentUser()!.username
let url = NSURL(string: "http://test.informatica-corlaer.nl/dropoffRecognizer.php?user=\(currentUsername!)&unique=3456364567")
let request = NSMutableURLRequest(URL: url!)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if data == nil {
print("request failed \(error)")
return
}
let parseError: NSError?
if let json = NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: String] {
let dropoffIndicator = json["dropoffIndicator"]
if (dropoffIndicator == "TRUE") {
dispatch_async(dispatch_get_main_queue()){
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let MainVC : UIViewController = Storyboard.instantiateViewControllerWithIdentifier("dropoffSuccess")
self.presentViewController(MainVC, animated: true, completion: nil)
}
}
} else {
print("parsing error: \(parseError)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("raw response: \(responseString)")
}
}
task.resume()
}
The problem is in the line if let json = NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: String]
. However, I am quite confused because I just got my code working with Swift 1.2. I really can't figure out how to fix this. What should I change?
I have tried all the other solutions from other similar questions, but I just can't get it to work. I tried to make a do-catch combination, but it only gave me more errors.
EDIT: New Code that I can't figure out
@IBAction func editingCodeChanged(sender: AnyObject) {
checkMaxLength(sender as! UITextField, maxLength: 4)
let currentUsername = PFUser.currentUser()!.username
if ((UnlockCodeField.text!).characters.count == 4) {
let url = NSURL(string: "http://test.informatica-corlaer.nl/unlockCodeTransmitter.php?user=\(currentUsername!)&unique=5782338593203")
let request = NSMutableURLRequest(URL: url!)
// modify the request as necessary, if necessary
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if data == nil {
print("request failed \(error)")
return
}
let parseError: NSError?
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: String] {
let databaseUnlockCode = json["unlockCode"]
let enteredUnlockCode = self.UnlockCodeField.text!
if (databaseUnlockCode == enteredUnlockCode) {
dispatch_async(dispatch_get_main_queue()){
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let MainVC : UIViewController = Storyboard.instantiateViewControllerWithIdentifier("VehiclePurchaseStatus")
self.presentViewController(MainVC, animated: true, completion: nil)
let myUrl = NSURL(string: "http://test.informatica-corlaer.nl/VEPunlockExecuter.php?user=\(currentUsername!)&unique=8648604386910");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
print(response)
}
}
} else {
dispatch_async(dispatch_get_main_queue()){
let alert = UIAlertView()
alert.title = "Whoops!"
alert.message = "You entered the wrong code. Please enter the code displayed on the VEP screen."
alert.addButtonWithTitle("OK")
alert.show()
}
}
} else {
print("parsing error: \(parseError)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("raw response: \(responseString)")
}
}
task.resume()
}
}