Bird
0
0

Which of the following is the correct way to check for an error in a URLSession data task completion handler?

easy📝 Syntax Q3 of 15
iOS Swift - Networking
Which of the following is the correct way to check for an error in a URLSession data task completion handler?
Aif let error = error { print(error.localizedDescription) }
Bif error == nil { print(error.localizedDescription) }
Cguard error = nil else { return }
Dif error != nil { return }
Step-by-Step Solution
Solution:
  1. Step 1: Understand optional binding for error

    Use if let to safely unwrap the optional error.
  2. Step 2: Check the correct syntax

    if let error = error { ... } unwraps error if it exists and allows handling.
  3. Final Answer:

    if let error = error { print(error.localizedDescription) } -> Option A
  4. Quick Check:

    Optional binding with if let = if let error = error { print(error.localizedDescription) } [OK]
Quick Trick: Use if let to unwrap error optionals safely [OK]
Common Mistakes:
  • Using assignment instead of comparison
  • Checking error == nil incorrectly
  • Using guard with wrong syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes