Bird
0
0

Identify the error in this Swift code using structured concurrency:

medium📝 Debug Q14 of 15
iOS Swift - Concurrency
Identify the error in this Swift code using structured concurrency:
func loadData() async {
  await withTaskGroup(of: String.self) { group in
    group.addTask { "First" }
    group.addTask { "Second" }
    for value in group {
      print(value)
    }
  }
}
AIncorrect type specified in withTaskGroup
Bgroup.addTask syntax is invalid
CMissing 'await' before 'for' loop to iterate group
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check iteration over task group

    Iterating over a task group requires 'for await' to await each task's result.
  2. Step 2: Identify missing 'await' keyword

    The code uses 'for value in group' instead of 'for await value in group', causing error.
  3. Final Answer:

    Missing 'await' before 'for' loop to iterate group -> Option C
  4. Quick Check:

    Use 'for await' to iterate async task groups [OK]
Quick Trick: Use 'for await' to loop over async task groups [OK]
Common Mistakes:
  • Using 'for' instead of 'for await' on task groups
  • Confusing task group type with task return type
  • Misusing addTask syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes