Bird
0
0

Identify the error in this Swift code for API integration:

medium📝 Debug Q14 of 15
iOS Swift - Networking
Identify the error in this Swift code for API integration:
let url = URL(string: "https://api.example.com")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
  if error != nil {
    print("Failed")
  } else {
    print("Success")
  }
}
AClosure parameters are incorrect
BURL string is invalid
CMissing task.resume() to start the request
DUsing URLSession.shared incorrectly
Step-by-Step Solution
Solution:
  1. Step 1: Check if the data task is started

    The code creates the task but never calls task.resume(), so the request never runs.
  2. Step 2: Confirm other parts are correct

    The URL is valid, closure parameters match, and URLSession usage is correct.
  3. Final Answer:

    Missing task.resume() to start the request -> Option C
  4. Quick Check:

    Without resume(), request won't run [OK]
Quick Trick: Always call resume() to start URLSession tasks [OK]
Common Mistakes:
  • Forgetting to call resume()
  • Force unwrapping URL without checking
  • Misnaming closure parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes