0
0
iOS Swiftmobile~10 mins

Await keyword in iOS Swift - Interactive Code Practice

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

Complete the code to wait for the asynchronous function to finish before printing the result.

iOS Swift
func fetchData() async -> String {
    return "Data loaded"
}

func load() async {
    let result = [1] fetchData()
    print(result)
}
Drag options to blanks, or click blank then click option'
Atry
Basync
Cdefer
Dawait
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' instead of 'await' before the function call.
Omitting the keyword and calling the function directly.
2fill in blank
medium

Complete the code to mark the function as asynchronous so it can use 'await'.

iOS Swift
func fetchUser() [1] -> String {
    return "User data"
}
Drag options to blanks, or click blank then click option'
Aawait
Bsync
Casync
Dthrows
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' to mark the function instead of 'async'.
Forgetting to mark the function as async when using await inside.
3fill in blank
hard

Fix the error in calling an async function inside an asynchronous function by adding the missing keyword.

iOS Swift
func syncFunction() async {
    let data = [1] fetchData()
    print(data)
}
Drag options to blanks, or click blank then click option'
Aawait
Bdefer
Ctry
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the async function without 'await', causing errors.
Using 'async' instead of 'await' before the call.
4fill in blank
hard

Fill both blanks to correctly define and call an async function that returns a string.

iOS Swift
func getMessage() [1] -> String {
    return "Hello"
}

func show() async {
    let message = [2] getMessage()
    print(message)
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Cthrows
Ddefer
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark the function as async.
Calling the async function without await.
5fill in blank
hard

Fill all three blanks to create an async function, call it with await, and handle errors with try.

iOS Swift
func fetchInfo() [1] throws -> String {
    return "Info"
}

func process() async {
    do {
        let info = [2] [3] fetchInfo()
        print(info)
    } catch {
        print("Error")
    }
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Ctry
Ddefer
Attempts:
3 left
💡 Hint
Common Mistakes
Using try without await or vice versa.
Not marking the function as async when it uses await.