Bird
0
0

Identify the error in this Swift async GET request code:

medium📝 Debug Q14 of 15
iOS Swift - Networking
Identify the error in this Swift async GET request code:
func load() async {
  let url = URL(string: "https://api.example.com")!
  let (data, response) = URLSession.shared.data(from: url)
  print(String(data: data, encoding: .utf8) ?? "No data")
}
AIncorrect print statement syntax
BURL is invalid
CMissing await keyword before URLSession.shared.data(from: url)
DMissing try keyword before URLSession.shared.data(from: url)
Step-by-Step Solution
Solution:
  1. Step 1: Check async call usage

    The method data(from:) is async and must be awaited with await.
  2. Step 2: Confirm missing await keyword

    The code calls URLSession.shared.data(from: url) without await, causing a compile error.
  3. Final Answer:

    Missing await keyword before URLSession.shared.data(from: url) -> Option C
  4. Quick Check:

    Async calls require await [OK]
Quick Trick: Async calls always need await keyword [OK]
Common Mistakes:
  • Forgetting await on async calls
  • Assuming try is optional
  • Misreading URL validity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes