title: Swift Rest API call example using Codable
body: <p>I am following a tutorial on REST API calls with Swift and Codable. I cannot compile the following although I was careful when I typed all of it. Can anyone tell me what's wrong? Also, can anyone point me to a better tutorial? The error is:</p>
  <blockquote>
  <p>Catch block is unreachable</p>
  </blockquote>
  <p>and also</p>
  <blockquote>
  <p>cannot find json in scope</p>
  </blockquote>
  <pre class="lang-swift prettyprint-override"><code>import UIKit
  import Foundation
  
  struct Example: Codable {
      let userId: Int
      let id: Int
      let title: String
      let completed: Bool
  }
  
  class ViewController: UIViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
      }
      
  
  
      func getJson(completion: @escaping (Example)-&gt; ()) {
          let urlString = &quot;https://jsonplaceholder.typicode.com/todos/1&quot;
          if let url = URL(string: urlString) {
              URLSession.shared.dataTask(with: url) {data, res, err in
                  if let data = data {
                      
                      let decoder = JSONDecoder()
                      do {
                          let json: Example = try! decoder.decode(Example.self, from: data)
                          completion(json)
                      }catch let error {
                          print(error.localizedDescription)
                      }
                  }
              }.resume()
          }
      }
  
      getJson() { (json) in
          print(json.id)
      }
  
  
  }
  </code></pre>