0
0
iOS Swiftmobile~20 mins

GET request with async/await in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Async GET Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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()
}
ARequest failed
BNo message found
CHello
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the JSON is parsed and the key accessed.
lifecycle
intermediate
2: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"
  }
}
AImmediately when the app launches
BWhen the view appears on screen
COnly when the user taps the Text
DWhen the view disappears
Attempts:
2 left
💡 Hint
The .task modifier runs code related to the view's lifecycle.
🔧 Debug
advanced
2: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")
}
AMissing await keyword before URLSession.shared.data call
BMissing try keyword before URLSession.shared.data call
CNo error, code runs fine
DURL is optional and not unwrapped safely
Attempts:
2 left
💡 Hint
Check how async functions are called with await.
🧠 Conceptual
advanced
1:30remaining
Which statement about async GET requests in Swift is true?
Choose the correct statement about using async/await for GET requests in Swift.
AAsync/await requires manually creating completion handlers
BAsync/await can only be used with POST requests
CAsync/await blocks the main thread until the request finishes
DAsync/await allows writing asynchronous code that looks like synchronous code
Attempts:
2 left
💡 Hint
Think about how async/await changes code style.
📝 Syntax
expert
2: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
  }
}
AAll of the above depending on server response
B0 if the server returns invalid data
C0 if the server returns empty data
D42 if the server returns "42" as plain text
Attempts:
2 left
💡 Hint
Consider how the code handles different server responses and errors.