Bird
0
0

What is wrong with this Swift code snippet?

medium📝 Debug Q7 of 15
iOS Swift - Concurrency
What is wrong with this Swift code snippet?
func fetchText() async -> String {
  return "Hello"
}

func printText() {
  let text = await fetchText()
  print(text)
}
Aawait cannot be used inside functions
BprintText must be marked async to use await
CfetchText cannot return String asynchronously
Dprint cannot print async function results
Step-by-Step Solution
Solution:
  1. Step 1: Understand await usage rules

    The await keyword can only be used inside async functions or async contexts.
  2. Step 2: Check printText function declaration

    Since printText() is not marked async, using await inside it causes an error.
  3. Final Answer:

    printText must be marked async to use await -> Option B
  4. Quick Check:

    Await requires async function = printText must be marked async to use await [OK]
Quick Trick: Only async functions can use await inside them [OK]
Common Mistakes:
  • Using await in non-async functions
  • Assuming await works anywhere
  • Forgetting to mark caller async

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes