0
import UIKit

struct Course : Decodable{
    let id: Int
    let name: String
    let link: String
    let imageUrl: String
}

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let url = "https://api.letsbuildthatapp.com/jsondecodable/course"

    let urlObj = URL(string: url)

    URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in

        do
        {
           let course = try JSONDecoder().decode(Course.self, from: data!)

            print(course.name)
        }
        catch
        {
            print(error)
        }

    }.resume()
  }
}

It works fine if I use .resume() but why is that? I have seen many peoples did without .resume() and also with the same code I have written.

dpapadopoulos
  • 1,834
  • 5
  • 23
  • 34
  • 1
    can you share link of code that using without .resume()? – Pratik Prajapati Feb 23 '19 at 11:52
  • `Newly-initialized tasks begin in a suspended state, so you need to call this .resume() method to start the task.` [Link](https://developer.apple.com/documentation/foundation/urlsessiontask/1411121-resume) – Pratik Prajapati Feb 23 '19 at 11:56

0 Answers0