上QQ阅读APP看书,第一时间看更新
Making your first call with URLSession
Let's take a look at the anatomy of a simple URL task:
let url = URL(string: "https://api.website.com/")!
let task = URLSession.shared.dataTask(url: url) { data, response, error in
if let error = error { return } // handle the error somehow
guard let response = response as HTTPURLResponse,
let data = data else { return }
// data: Data is set, and all good
}
task.resume()
If the call has succeeded, the data variable will be set and will contain the data downloaded from the remote server.