Complete the code to create a default URLSession instance.
let session = URLSession.[1]The shared property gives you a shared singleton URLSession instance for simple tasks.
Complete the code to create a URL from a string.
guard let url = URL(string: [1]) else { return }
The URL(string:) initializer expects a string literal or variable containing the URL string.
Fix the error in the code to start a data task.
let task = session.dataTask(with: url) [1]
You must call resume() on the task to start it. Other methods do not exist.
Fill both blanks to handle the data and error in the completion handler.
let task = session.dataTask(with: url) { data, response, [1] in if let error = [2] { print(error.localizedDescription) } }
The completion handler parameter for errors is usually named error. You check it with if let error = error.
Fill all three blanks to safely unwrap data and convert it to a string.
let task = session.dataTask(with: url) { data, response, error in guard let [1] = data, error == nil else { return } let result = String(data: [2], encoding: [3]) print(result ?? "No data") }
You unwrap data safely, then use it to create a string with UTF-8 encoding.
