Bird
0
0

Identify the error in this Swift code using TaskGroup:

medium📝 Debug Q6 of 15
iOS Swift - Concurrency
Identify the error in this Swift code using TaskGroup:
func fetchAll() async {
  let group = TaskGroup()
  group.addTask { 10 }
  group.addTask { 20 }
  for await value in group {
    print(value)
  }
}
ATaskGroup cannot be instantiated directly; use withTaskGroup instead
BMissing await keyword before addTask calls
Cfor await loop cannot be used with TaskGroup
DaddTask must return Void, not Int
Step-by-Step Solution
Solution:
  1. Step 1: Check TaskGroup creation

    TaskGroup is not created by direct init; use withTaskGroup function.
  2. Step 2: Confirm correct usage

    withTaskGroup provides a safe scope to add and await tasks.
  3. Final Answer:

    TaskGroup cannot be instantiated directly; use withTaskGroup instead -> Option A
  4. Quick Check:

    TaskGroup creation = withTaskGroup function [OK]
Quick Trick: Use withTaskGroup, not direct TaskGroup() init [OK]
Common Mistakes:
  • Trying to create TaskGroup with init()
  • Forgetting to await tasks
  • Misusing for-await loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes