0
0
iOS Swiftmobile~10 mins

Task and TaskGroup 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 create a simple asynchronous task that prints a message.

iOS Swift
Task {
    print([1])
}
Drag options to blanks, or click blank then click option'
ATask.sleep(1_000_000_000)
BHello from Task!
Cprint("Hello from Task!")
D"Hello from Task!"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Trying to print without a string argument.
2fill in blank
medium

Complete the code to create a TaskGroup that runs two tasks concurrently.

iOS Swift
Task {
    await withTaskGroup(of: Void.self) { group in
        group.addTask {
            print("Task 1")
        }
        group.addTask {
            print([1])
        }
    }
}
Drag options to blanks, or click blank then click option'
Aprint("Task 2")
BTask 2
C"Task 2"
DTask.sleep(1_000_000_000)
Attempts:
3 left
💡 Hint
Common Mistakes
Missing quotes around the string.
Trying to call print inside print.
3fill in blank
hard

Fix the error in the TaskGroup code by completing the blank with the correct keyword to wait for all tasks.

iOS Swift
Task {
    [1] withTaskGroup(of: Int.self) { group in
        group.addTask { 1 }
        group.addTask { 2 }
    }
}
Drag options to blanks, or click blank then click option'
Aawait
Basync
Ctry
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' instead of 'await'.
Omitting the keyword entirely.
4fill in blank
hard

Fill both blanks to create a TaskGroup that returns the sum of two asynchronous tasks.

iOS Swift
let sum = await withTaskGroup(of: Int.self) { group in
    group.addTask { 5 }
    group.addTask { 10 }
    var total = 0
    for await [1] in group {
        total [2] [1]
    }
    return total
}
Drag options to blanks, or click blank then click option'
Avalue
B+=
C-=
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' instead of '+='.
Using a loop variable name not matching the code.
5fill in blank
hard

Fill all three blanks to create a TaskGroup that returns the maximum value from three asynchronous tasks.

iOS Swift
let maxValue = await withTaskGroup(of: Int.self) { group in
    group.addTask { 3 }
    group.addTask { 7 }
    group.addTask { 5 }
    var maxVal = Int.min
    for await [1] in group {
        if [1] [2] maxVal {
            maxVal = [1]
        }
    }
    return maxVal
}
Drag options to blanks, or click blank then click option'
Aresult
B>
C<
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the comparison.
Using inconsistent variable names.