json - Swift Errors Thrown from here are not handled -
i have had code working in xcode 6 since got xcode 7 cannot figure out how fix this. let jsonresult line has error says error thrown here not handled. code 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
if @ definition of jsonobjectwithdata
method in swift 2 throws error.
class func jsonobjectwithdata(data: nsdata, options opt: nsjsonreadingoptions) throws -> anyobject
in swift 2 if function throws error have handle do-try-catch block
here how works
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 } catch { // handle error } }
or if don't want handle error can force try!
keyword.
let jsonresult:nsdictionary = try! nsjsonserialization.jsonobjectwithdata(self.bytes, options: nsjsonreadingoptions.mutablecontainers) as! nsdictionary print(jsonresult)
as other keywords ends ! risky operation. if there error program crash.
Comments
Post a Comment