Bird
0
0

What is the issue with this Swift async GET request code?

medium📝 Debug Q6 of 15
iOS Swift - Networking
What is the issue with this Swift async GET request code?
func fetchData() async {
  let url = URL(string: "https://example.com")!
  let (data, response) = URLSession.shared.data(from: url)
  print(data)
}
AMissing both 'try' and 'await' keywords before the data call
BURL is not unwrapped safely
CThe function should not be marked async
DThe print statement should be inside a do-catch block
Step-by-Step Solution
Solution:
  1. Step 1: Identify async call requirements

    The method URLSession.shared.data(from:) is asynchronous and throwable, so it requires try and await.
  2. Step 2: Check the code

    The code calls the method without try and await, which is incorrect.
  3. Final Answer:

    Missing both 'try' and 'await' keywords before the data call -> Option A
  4. Quick Check:

    Async throwable calls need both try and await [OK]
Quick Trick: Async throwable calls require try and await keywords [OK]
Common Mistakes:
  • Forgetting await on async calls
  • Forgetting try on throwable calls
  • Assuming async functions are synchronous

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes