Bird
0
0

What is the issue with this Swift code?

medium📝 Debug Q6 of 15
iOS Swift - Concurrency
What is the issue with this Swift code?
func retrieveData() async -> String {
  return "Info"
}

func displayData() {
  let info = await retrieveData()
  print(info)
}
AThe await keyword is not needed before retrieveData().
BThe function retrieveData() should not be async.
CThe function displayData() is not marked async but uses await.
DThe print statement cannot be used inside async functions.
Step-by-Step Solution
Solution:
  1. Step 1: Check async context

    The function displayData() calls an async function with await but is not declared async.
  2. Step 2: Swift rules

    Using await requires the enclosing function to be async.
  3. Final Answer:

    The function displayData() is not marked async but uses await. -> Option C
  4. Quick Check:

    Await requires async function [OK]
Quick Trick: Await only inside async functions [OK]
Common Mistakes:
  • Forgetting to mark functions async when using await
  • Removing await before async calls
  • Misunderstanding async function declarations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes