Bird
0
0

Which Swift code snippet correctly performs an asynchronous GET request using async/await?

easy📝 Syntax Q3 of 15
iOS Swift - Networking
Which Swift code snippet correctly performs an asynchronous GET request using async/await?
Alet (data, response) = try await URLSession.shared.data(from: url)
Blet (data, response) = URLSession.shared.data(from: url)
Clet (data, response) = try URLSession.shared.data(from: url)
Dlet (data, response) = await URLSession.shared.data(from: url)
Step-by-Step Solution
Solution:
  1. Step 1: Use async/await keywords

    The method URLSession.shared.data(from:) is asynchronous and requires both try and await to handle errors and await the result.
  2. Step 2: Correct syntax

    let (data, response) = try await URLSession.shared.data(from: url) correctly uses try await before the call.
  3. Final Answer:

    let (data, response) = try await URLSession.shared.data(from: url) -> Option A
  4. Quick Check:

    Check for both try and await keywords [OK]
Quick Trick: Always use both try and await for async URLSession calls [OK]
Common Mistakes:
  • Omitting try keyword
  • Omitting await keyword
  • Using synchronous data(from:) method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes