Bird
0
0

Which of the following is the correct way to create a data task with URLSession?

easy📝 Syntax Q12 of 15
iOS Swift - Networking
Which of the following is the correct way to create a data task with URLSession?
Alet task = URLSession.dataTask(url) { data, error in }
Blet task = URLSession.shared.dataTask(url)
Clet task = URLSession.shared.downloadTask(url)
Dlet task = URLSession.shared.dataTask(with: url) { data, response, error in }
Step-by-Step Solution
Solution:
  1. Step 1: Check correct method signature

    The correct method is dataTask(with:completionHandler:) on URLSession.shared.
  2. Step 2: Validate options

    let task = URLSession.shared.dataTask(with: url) { data, response, error in } matches the correct syntax with closure parameters data, response, error. Others have wrong method names or missing parameters.
  3. Final Answer:

    let task = URLSession.shared.dataTask(with: url) { data, response, error in } -> Option D
  4. Quick Check:

    Correct dataTask syntax = let task = URLSession.shared.dataTask(with: url) { data, response, error in } [OK]
Quick Trick: Look for shared session and closure with three parameters [OK]
Common Mistakes:
  • Using dataTask without shared session
  • Missing response parameter in closure
  • Using downloadTask instead of dataTask

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes