I am trying to convert my code over to Swift 2 and I am having issues with this last function. I get the follow error on the declaration of task:
Invalid conversion from throwing function of type ... to non throwing type.
func performSearch(searchTerm: String){
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
do {
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
let results: AnyObject? = result.objectForKey("results")
if let testVar: NSMutableArray = results as? NSMutableArray {
// Uncomment this to print all feeds
//println(testVar)
self.searchedPodcasts.removeAllObjects()
self.searchedPodcasts.addObjectsFromArray(testVar as [AnyObject])
NSNotificationCenter.defaultCenter().postNotificationName("SearchPerformed", object: self)
}
})
task!.resume()
}
I have tried the following but it doesn't work either and it also means I can't call task.resume for some reason:
func performSearch(searchTerm: String){
let session = NSURLSession.sharedSession()
do {
let task = session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
do {
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
let results: AnyObject? = result.objectForKey("results")
if let testVar: NSMutableArray = results as? NSMutableArray {
// Uncomment this to print all feeds
//println(testVar)
self.searchedPodcasts.removeAllObjects()
self.searchedPodcasts.addObjectsFromArray(testVar as [AnyObject])
NSNotificationCenter.defaultCenter().postNotificationName("SearchPerformed", object: self)
}
})
} catch {
print("error)
}
task!.resume()
}
What can I do to fix it?
Edit: Tried this but the same error is still returned.
func test(searchTerm: String) {
let session = NSURLSession.sharedSession()
do {
let task = try session.dataTaskWithURL(urlForQuery(searchTerm)!, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
})
} catch {
print("hello")
}
}