Challenge - 5 Problems
Swift Async GET Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Swift async GET request code?
Consider this Swift code that fetches JSON data asynchronously. What will be printed if the request succeeds and the JSON contains {"message": "Hello"}?
iOS Swift
import Foundation func fetchMessage() async { guard let url = URL(string: "https://example.com/api/message") else { return } do { let (data, _) = try await URLSession.shared.data(from: url) if let json = try? JSONSerialization.jsonObject(with: data) as? [String: String], let message = json["message"] { print(message) } else { print("No message found") } } catch { print("Request failed") } } Task { await fetchMessage() }
Attempts:
2 left
💡 Hint
Look at how the JSON is parsed and the key accessed.
✗ Incorrect
The code fetches data from the URL, parses it as JSON dictionary, then prints the value for key "message". Since the JSON contains {"message": "Hello"}, it prints "Hello".
❓ lifecycle
intermediate2:00remaining
When is the async GET request executed in this SwiftUI view?
Given this SwiftUI view, when does the fetchData() async function run?
iOS Swift
import SwiftUI struct ContentView: View { @State private var text = "" var body: some View { Text(text) .task { await fetchData() } } func fetchData() async { // async GET request code here text = "Data loaded" } }
Attempts:
2 left
💡 Hint
The .task modifier runs code related to the view's lifecycle.
✗ Incorrect
The .task modifier runs its async code when the view appears on screen, so fetchData() runs then.
🔧 Debug
advanced2:00remaining
What error does this async GET request code produce?
Identify the error in this Swift async GET request code snippet.
iOS Swift
func loadData() async { let url = URL(string: "https://example.com/data")! let (data, response) = try await URLSession.shared.data(from: url) print(String(data: data, encoding: .utf8) ?? "No data") }
Attempts:
2 left
💡 Hint
Check how async functions are called with await.
✗ Incorrect
URLSession.shared.data(from:) is async and must be called with await. Missing await causes a compile error.
🧠 Conceptual
advanced1:30remaining
Which statement about async GET requests in Swift is true?
Choose the correct statement about using async/await for GET requests in Swift.
Attempts:
2 left
💡 Hint
Think about how async/await changes code style.
✗ Incorrect
Async/await lets you write asynchronous code in a clear, linear style without callbacks.
📝 Syntax
expert2:30remaining
What is the value of 'result' after this Swift async GET request code runs?
Given this code snippet, what will be the value of 'result' after execution?
iOS Swift
import Foundation func fetchNumber() async throws -> Int { let url = URL(string: "https://example.com/number")! let (data, _) = try await URLSession.shared.data(from: url) let numberString = String(data: data, encoding: .utf8) ?? "0" return Int(numberString) ?? 0 } var result = 0 Task { do { result = try await fetchNumber() } catch { result = -1 } }
Attempts:
2 left
💡 Hint
Consider how the code handles different server responses and errors.
✗ Incorrect
The code returns the integer parsed from server data, or 0 if parsing fails, or -1 if the request throws an error. So result depends on server response.