0
0
iOS Swiftmobile~10 mins

Why async/await simplifies concurrent code in iOS Swift - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an asynchronous function in Swift.

iOS Swift
func fetchData() [1] {
  // code to fetch data
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Cthrows
Dsync
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' instead of 'async' in the function declaration.
Using 'throws' which is for error handling, not async behavior.
2fill in blank
medium

Complete the code to call an asynchronous function using await.

iOS Swift
func load() async {
  let data = try? [1] fetchData()
}
Drag options to blanks, or click blank then click option'
Aawait
Basync
Casync await
Dtry
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' before the function call instead of 'await'.
Omitting the 'await' keyword causing compile errors.
3fill in blank
hard

Fix the error in the code by adding the missing keyword to handle asynchronous code.

iOS Swift
func process() [1] {
  let result = try? await fetchData()
}
Drag options to blanks, or click blank then click option'
Async
Bawait
Cthrows
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark the function as async causes compile errors.
Trying to use await inside a synchronous function.
4fill in blank
hard

Fill both blanks to create a task that runs asynchronous code concurrently.

iOS Swift
Task.[1] {
  let data = try? [2] fetchData()
}
Drag options to blanks, or click blank then click option'
Adetached
Bawait
Casync
Dsync
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' instead of 'await' inside the task.
Not using 'detached' to create a concurrent task.
5fill in blank
hard

Fill all three blanks to define an async function that returns a string after awaiting another async call.

iOS Swift
func getMessage() [1] [2] -> String {
  let data = try? [3] fetchData()
  return data ?? "No data"
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Cthrows
Dsync
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark the function as async and throws.
Not using await before the async function call.