Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors thrown from here are not handled

I've got this problem trying to parse a JSON on my iOS app:

Relevant code:

let jsonData:NSDictionary = try JSONSerialization.jsonObject(with: urlData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers ) as! NSDictionary  /* XCode error ^^^ Errors thrown from here are not handled */ 

Could anybody help me ?

like image 283
TibiaZ Avatar asked Nov 07 '16 16:11

TibiaZ


1 Answers

A possibly thrown error in let jsonData = try JSONSerialization ... is not handled.

You can ignore a possible error, and crash as penalty if an error occurs:

let jsonData = try! JSONSerialization ... 

or return an Optional, so jsonData is nil in error case:

let jsonData = try? JSONSerialization ... 

or you can catch and handle the thrown error:

do {     let jsonData = try JSONSerialization ...     //all fine with jsonData here } catch {     //handle error     print(error) } 

You might want to study The Swift Language

like image 145
shallowThought Avatar answered Sep 25 '22 07:09

shallowThought