Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an async function.
Swift
func fetchData() async [1] String { return "Data" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' instead of '->' to specify return type
Using 'throws' when not throwing errors
✗ Incorrect
The arrow -> is used to specify the return type of the function.
2fill in blank
mediumComplete the code to call an async function with await.
Swift
Task {
let result = try? await [1]()
print(result ?? "No data")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses inside the await expression
Calling Task instead of the function
✗ Incorrect
You call the async function by its name without parentheses inside await.
3fill in blank
hardFix the error in the async test function declaration.
Swift
func testFetch() [1] async throws { let data = try await fetchData() XCTAssertEqual(data, "Data") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Marking only async or only throws
Forgetting to mark the test function as async
✗ Incorrect
Test functions that call async throwing functions must be marked async throws.
4fill in blank
hardFill both blanks to create a dictionary comprehension filtering async results.
Swift
let filtered = try await Dictionary(uniqueKeysWithValues: results.compactMap { item in guard let value = item.value, value [1] 10 else { return nil } return (item.key, value[2]2) })
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the guard
Using ** instead of * for multiplication
✗ Incorrect
The guard checks if value is greater than 10, and the value is multiplied by 2 using *.
5fill in blank
hardFill all three blanks to create an async test that awaits and asserts a value.
Swift
func testValue() async throws {
let value = try await [1]()
XCTAssertEqual(value, [2])
XCTAssertTrue(value [3] 0)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetchData instead of fetchNumber
Using wrong expected value in XCTAssertEqual
✗ Incorrect
The function fetchNumber() is awaited, the expected value is "Data", and the test checks if value is greater than 0.