Bird
0
0

Which of the following is the correct way to create and start a URLSession data task in Swift?

easy📝 Syntax Q3 of 15
iOS Swift - Networking
Which of the following is the correct way to create and start a URLSession data task in Swift?
AURLSession.shared.dataTask(with: url).begin()
Blet task = URLSession.shared.dataTask(with: url) { data, response, error in /* handle */ }
CURLSession.shared.dataTask(with: url).start()
Dlet task = URLSession.shared.dataTask(with: url) { data, response, error in /* handle */ }; task.resume()
Step-by-Step Solution
Solution:
  1. Step 1: Create the data task

    Use URLSession.shared.dataTask(with:completionHandler:) to create the task.
  2. Step 2: Start the task

    Call task.resume() to start the network request.
  3. Final Answer:

    let task = URLSession.shared.dataTask(with: url) { data, response, error in /* handle */ }; task.resume() correctly creates and starts the task.
  4. Quick Check:

    Always call resume() to start the task [OK]
Quick Trick: Always call resume() after creating a data task [OK]
Common Mistakes:
  • Forgetting to call resume()
  • Using non-existent start() or begin() methods
  • Assuming task starts automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes