Bird
0
0

Why does this code cause a compile error?

medium📝 Debug Q7 of 15
iOS Swift - Concurrency
Why does this code cause a compile error?
for number in asyncStream {
  print(number)
}
Aprint() cannot be used inside async loops
BAsyncStream cannot be iterated with for loops
CMissing 'await' keyword in the for loop
DasyncStream must be awaited before the loop
Step-by-Step Solution
Solution:
  1. Step 1: Identify async iteration syntax

    Async sequences require 'for await' to iterate asynchronously.
  2. Step 2: Check code for missing keyword

    The code uses 'for' without 'await', causing a compile error.
  3. Final Answer:

    Missing 'await' keyword in the for loop -> Option C
  4. Quick Check:

    'for await' needed for async sequence loops [OK]
Quick Trick: Always use 'for await' with async sequences [OK]
Common Mistakes:
  • Using 'for' without 'await'
  • Thinking AsyncStream is synchronous
  • Misunderstanding print usage in async loops

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes