0
0
iOS Swiftmobile~10 mins

Structured concurrency in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start an asynchronous task using Swift's structured concurrency.

iOS Swift
Task {
    await [1]()
}
Drag options to blanks, or click blank then click option'
Aprint
Bsleep
CfetchData
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use await before the async function call.
Using a synchronous function inside Task without await.
2fill in blank
medium

Complete the code to define an async function that returns a string after a delay.

iOS Swift
func fetchData() async -> String {
    try? await Task.sleep(nanoseconds: 1_000_000_000)
    return [1]
}
Drag options to blanks, or click blank then click option'
ATask.sleep
B"Done"
Cawait "Done"
DDone
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a bare word without quotes causes an error.
Trying to await a string literal.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword to mark the function as asynchronous.

iOS Swift
func loadData() [1] -> String {
    return "Loaded"
}
Drag options to blanks, or click blank then click option'
Aasynchronous
Bawait
Casync await
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' instead of 'async' in the function signature.
Writing 'async await' together which is invalid.
4fill in blank
hard

Fill both blanks to create a task group that runs two async functions concurrently and collects their results.

iOS Swift
await withTaskGroup(of: String.self) { group in
    group.[1] {
        await fetchData1()
    }
    group.[2] {
        await fetchData2()
    }
}
Drag options to blanks, or click blank then click option'
AaddTask
BrunTask
CstartTask
DlaunchTask
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods that do not exist on TaskGroup.
Using different methods for each task.
5fill in blank
hard

Fill all three blanks to create a dictionary that maps task results to their lengths, filtering only results longer than 3 characters.

iOS Swift
let results = ["apple", "bat", "carrot"]
let lengths = results.reduce(into: [String: Int]()) { lengths, [1] in
    if [2] > 3 {
        lengths[[3]] = [2]
    }
}
Drag options to blanks, or click blank then click option'
Aword
Bword.count
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables.
Mixing up keys and values in the dictionary.