I have this code
let path : String = "http://apple.com" let lookupURL : NSURL = NSURL(string:path)! let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(lookupURL, completionHandler: {(data, reponse, error) in let jsonResults : AnyObject do { jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: []) // success ... } catch let error as NSError { // failure print("Fetch failed: \(error.localizedDescription)") } // do something }) task.resume()
but it does not work on the let task line with an error:
an invalid conversion from a cast function of type (__.__.__) throws the type of a non-throwing function (NSData ?, NSURLResponse ?, NSError?) → Void
what's wrong? These are Xcode 7 beta 4, iOS 9 and Swift 2.
edit:
the problem seems to be related to these lines
do { jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: []) // success ... } catch let error as NSError { // failure print("Fetch failed: \(error.localizedDescription)") }
I delete these lines and the let task error disappears.
source share