What does async/await help you do in Swift networking?
Async/await lets you write asynchronous code that looks like normal, easy-to-read code. It helps you wait for network responses without blocking the app.
Click to reveal answer
beginner
Which Swift type is used to make a GET request asynchronously?
URLSession.shared.data(from:) is used with async/await to make GET requests and get data and response.
Click to reveal answer
intermediate
What is the purpose of 'try' in 'let (data, response) = try await URLSession.shared.data(from: url)'?
'Try' is used because the data task can throw an error if the request fails, so you must handle or propagate errors.
Click to reveal answer
intermediate
How do you check if the GET request was successful in Swift?
Check if the response is an HTTPURLResponse and its statusCode is 200 (OK) to confirm success.
Click to reveal answer
beginner
Why is it important to use async/await instead of completion handlers for GET requests?
Async/await makes code easier to read and maintain by avoiding nested closures and callback hell.
Click to reveal answer
Which Swift method is used with async/await to perform a GET request?
URLSession.shared.data(from:) is designed for async/await usage to fetch data from a URL.
What keyword do you use to wait for a network call to finish in Swift async/await?
Await
Bawait
Cpause
Dasync
✗ Incorrect
'await' pauses the function until the asynchronous call completes.
What does the 'try' keyword indicate in Swift async calls?
AThe call might throw an error
BThe call will never fail
CThe call is synchronous
DThe call is optional
✗ Incorrect
'try' means the function can throw an error, so you must handle it.
How do you confirm a successful HTTP GET response in Swift?
ACheck if statusCode == 404
BCheck if statusCode == 301
CCheck if statusCode == 500
DCheck if statusCode == 200
✗ Incorrect
Status code 200 means the request was successful.
Why prefer async/await over completion handlers for GET requests?
AAsync/await is slower
BAsync/await is harder to read
CAsync/await avoids nested callbacks
DAsync/await requires more code
✗ Incorrect
Async/await makes asynchronous code look simpler and avoids callback nesting.
Explain how to perform a GET request using async/await in Swift.
Think about how to wait for data and handle errors.
You got /4 concepts.
Describe why async/await improves code readability compared to completion handlers in networking.
Imagine reading a story versus a maze.
You got /4 concepts.
Practice
(1/5)
1. What does the async keyword indicate in a Swift function that performs a GET request?
easy
A. The function is synchronous and blocks the main thread.
B. The function runs asynchronously and can be awaited.
C. The function automatically retries on failure.
D. The function only works with POST requests.
Solution
Step 1: Understand async keyword meaning
The async keyword means the function runs asynchronously, allowing other code to run while waiting.
Step 2: Connect async with await usage
Functions marked async can be called with await to pause until the result is ready without blocking.
Final Answer:
The function runs asynchronously and can be awaited. -> Option B
Quick Check:
async means asynchronous function [OK]
Hint: async means function can be paused with await [OK]
Common Mistakes:
Thinking async means synchronous/blocking
Confusing async with retry logic
Assuming async only applies to POST requests
2. Which of the following is the correct syntax to perform a GET request using async/await in Swift?
easy
A. let (data, _) = try await URLSession.shared.data(from: url)
B. let data = URLSession.shared.dataTask(with: url)
C. let data = await URLSession.shared.data(from: url)
D. let (data, response) = URLSession.shared.data(from: url)
Solution
Step 1: Identify correct async/await syntax
The correct syntax uses try await with URLSession.shared.data(from:) to get data and response.
Step 2: Check tuple unpacking
The method returns a tuple of (Data, URLResponse), so unpacking with (data, _) is correct if response is unused.
Final Answer:
let (data, _) = try await URLSession.shared.data(from: url) -> Option A
Quick Check:
Use try await with data(from:) [OK]
Hint: Use try await with data(from:) and unpack tuple [OK]
Common Mistakes:
Omitting try keyword
Using dataTask without await
Not unpacking the returned tuple
3. Given the code below, what will be printed if the GET request succeeds?
func fetchData() async {
let url = URL(string: "https://example.com/data.json")!
do {
let (data, _) = try await URLSession.shared.data(from: url)
print("Data length: \(data.count)")
} catch {
print("Failed to fetch")
}
}
medium
A. Syntax error
B. Failed to fetch
C. Data length: 0
D. Data length: [number of bytes in data]
Solution
Step 1: Analyze successful GET request
If the request succeeds, try await returns data and response without throwing an error.
Step 2: Understand print statement output
The code prints the length of the received data in bytes, so output is "Data length: [number]".
Final Answer:
Data length: [number of bytes in data] -> Option D
Quick Check:
Success prints data length [OK]
Hint: Success prints data length, failure prints error [OK]
Common Mistakes:
Assuming print shows raw data
Confusing catch block output
Expecting zero length on success
4. 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")
}
medium
A. Incorrect print statement syntax
B. URL is invalid
C. Missing await keyword before URLSession.shared.data(from: url)
D. Missing try keyword before URLSession.shared.data(from: url)
Solution
Step 1: Check async call usage
The method data(from:) is async and must be awaited with await.
Step 2: Confirm missing await keyword
The code calls URLSession.shared.data(from: url) without await, causing a compile error.
Final Answer:
Missing await keyword before URLSession.shared.data(from: url) -> Option C
Quick Check:
Async calls require await [OK]
Hint: Async calls always need await keyword [OK]
Common Mistakes:
Forgetting await on async calls
Assuming try is optional
Misreading URL validity
5. You want to fetch JSON data from a URL and decode it into a Swift struct using async/await. Which code snippet correctly performs the GET request and decodes the data?
struct User: Decodable {
let id: Int
let name: String
}
func fetchUser() async throws -> User {
let url = URL(string: "https://api.example.com/user")!
// Which code goes here?
}
hard
A. let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode(User.self, from: data)
B. let data = try URLSession.shared.data(from: url)
return JSONDecoder().decode(User.self, from: data)
C. let (data, _) = URLSession.shared.data(from: url)
return try JSONDecoder().decode(User.self, from: data)
D. let (data, _) = try await URLSession.shared.dataTask(with: url)
return try JSONDecoder().decode(User.self, from: data)
Solution
Step 1: Use async/await with data(from:)
The correct async call is try await URLSession.shared.data(from: url) which returns data and response.
Step 2: Decode JSON data into struct
Use JSONDecoder().decode(User.self, from: data) to convert JSON bytes into the User struct.
Final Answer:
let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode(User.self, from: data) -> Option A
Quick Check:
Use try await with data(from:) and decode JSON [OK]
Hint: Use try await data(from:) then decode JSON [OK]
Common Mistakes:
Using dataTask instead of data(from:)
Omitting await or try keywords
Not decoding JSON properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.