Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Errors Thrown from here are not handled

I have had my code working in Xcode 6 but since I got Xcode 7 I cannot figure out how to fix this. The let jsonresult line has an error that says Error thrown from here are not handled. Code is below:

func connectionDidFinishLoading(connection: NSURLConnection!) {

    let jsonresult:NSDictionary = try NSJSONSerialization.JSONObjectWithData(self.bytes, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

    print(jsonresult)

    let number:Int = jsonresult["count"] as! Int

    print(number)

    numberElements = number

    let results: NSDictionary = jsonresult["results"] as! NSDictionary

    let collection1: NSArray = results["collection1"] as! NSArray

Thanks

like image 900
Nathan Schafer Avatar asked Jun 13 '15 09:06

Nathan Schafer


People also ask

What is error handling in Swift?

Error handling is the process of responding to and recovering from error conditions in your program. Swift provides first-class support for throwing, catching, propagating, and manipulating recoverable errors at runtime. Some operations aren't guaranteed to always complete execution or produce a useful output.

How do you handle a try in Swift?

try – You must use this keyword in front of the method that throws. Think of it like this: “You're trying to execute the method. catch – If the throwing method fails and raises an error, the execution will fall into this catch block. This is where you'll write code display a graceful error message to the user.

How do I create a custom error in Swift?

description for Custom Errors Using CustomStringConvertible // For each error type return the appropriate description extension CustomError: CustomStringConvertible { public var description: String { switch self { case . invalidPassword: return "The provided password is not valid." case .


1 Answers

If you look at the definition of JSONObjectWithData method in swift 2 it throws error.

class func JSONObjectWithData(data: NSData, options opt: NSJSONReadingOptions) throws -> AnyObject

In swift 2 if some function throws an error you have to handle it with do-try-catch block

Here is how it works

func connectionDidFinishLoading(connection: NSURLConnection!) {
    do {
        let jsonresult:NSDictionary = try NSJSONSerialization.JSONObjectWithData(self.bytes, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
        print(jsonresult)

        let number:Int = jsonresult["count"] as! Int

        print(number)

        numberElements = number

        let results: NSDictionary = jsonresult["results"] as! NSDictionary

        let collection1: NSArray = results["collection1"] as! NSArray
    } catch {
        // handle error
    }
}

Or if you don't want handle error you can force it with try! keyword.

let jsonresult:NSDictionary = try! NSJSONSerialization.JSONObjectWithData(self.bytes, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
        print(jsonresult)

As with other keywords that ends ! this is a risky operation. If there is an error your program will crash.

like image 59
mustafa Avatar answered Sep 29 '22 13:09

mustafa