I have marked my function with throws, why does swift force me to use a do-try-catch block?
I want to handle any kind of error thrown where I call this function below.
static func getPosts() throws {
let url = URL(string: "https://jsonplaceholder.typicode.com/posts/1")
let request = URLRequest(url: url!)
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableLeaves) as! [String: Any]
}.resume()
}
Below is screen-shot of error I am getting.