user2747220
user2747220

Reputation: 893

Invalid conversion from throwing function of type

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")
        }
    }

Upvotes: 1

Views: 3361

Answers (2)

Gerd Castan
Gerd Castan

Reputation: 6849

Apple replaced NSError with ErrorType in Swift 2.

So replace your own explicit usage of NSError with ErrorType and you don't need the additional try catch block suggested in the other answer and you can express your error handling with much less code.

Upvotes: 1

redent84
redent84

Reputation: 19239

Try adding a catch block to your do-try-catch sentence:

do {
    let result: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
}
catch {
    print("error");
}

You'll also need to declare the result variable outside or move more code inside the do block.

For more info about exception handling in Swift 2 check out this guide.

Upvotes: 3

Related Questions