0
0
iOS Swiftmobile~10 mins

Async functions 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 declare an async function named fetchData.

iOS Swift
func fetchData() [1] {
    // function body
}
Drag options to blanks, or click blank then click option'
Async
Bawait
Cthrows
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' instead of 'async' to declare the function.
Forgetting to mark the function as async.
2fill in blank
medium

Complete the code to call an async function fetchData and wait for its result.

iOS Swift
Task {
    let data = try? await [1]()
    print(data ?? "No data")
}
Drag options to blanks, or click blank then click option'
AfetchData
Bfetch
CgetData
DloadData
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the function without 'await'.
Using a wrong function name.
3fill in blank
hard

Fix the error in the async function declaration by filling the blank.

iOS Swift
func loadData() [1] throws {
    // function body
}
Drag options to blanks, or click blank then click option'
Aawait
Bsync
Casync
Dthrows async
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' instead of 'async' in the declaration.
Combining 'throws' and 'async' incorrectly.
4fill in blank
hard

Fill both blanks to declare and call an async function correctly.

iOS Swift
func fetchUser() [1] {
    // fetch user data
}

Task {
    let user = try? await [2]()
    print(user ?? "No user")
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
CfetchUser
DloadUser
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark the function as async.
Calling the function without 'await'.
Using the wrong function name.
5fill in blank
hard

Fill all three blanks to create an async function that throws and call it correctly.

iOS Swift
func fetchData() [1] [2] {
    // fetch data
}

Task {
    do {
        let data = try [3] fetchData()
        print(data)
    } catch {
        print("Error")
    }
}
Drag options to blanks, or click blank then click option'
Aasync
Bthrows
Cawait
Dtry
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'async' or 'throws' in the function declaration.
Calling the function without 'await'.
Forgetting to use 'try' when calling a throwing function.