Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
iOS Swift - Concurrency
Identify the error in this code snippet:
func stream() async -> AsyncStream {
  print("Stream started")
  AsyncStream { continuation in
    continuation.yield(1)
    continuation.finish()
  }
}
AAsyncStream cannot be returned from async functions
BMissing 'return' keyword before AsyncStream
CContinuation must yield values asynchronously
Dfinish() must be called outside the closure
Step-by-Step Solution
Solution:
  1. Step 1: Check function return statement

    The function declares a return type but does not use 'return' to return the AsyncStream instance.
  2. Step 2: Confirm correct syntax

    Adding 'return' before AsyncStream fixes the error.
  3. Final Answer:

    Missing 'return' keyword before AsyncStream -> Option B
  4. Quick Check:

    Functions must 'return' values explicitly [OK]
Quick Trick: Always use 'return' to output values from functions [OK]
Common Mistakes:
  • Omitting 'return' in functions
  • Thinking AsyncStream can't be returned async
  • Misplacing finish() call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes