Bird
0
0

Which of the following is the correct syntax to safely unwrap an Optional age: Int? using if let?

easy📝 Syntax Q12 of 15
iOS Swift - Swift Language Essentials
Which of the following is the correct syntax to safely unwrap an Optional age: Int? using if let?
Aif let unwrappedAge = age { print(unwrappedAge) }
Bif age != nil { print(age) }
Cif let age = age? { print(age) }
Dif age = nil { print(age) }
Step-by-Step Solution
Solution:
  1. Step 1: Recall safe unwrapping syntax

    Safe unwrapping uses if let to bind the Optional value if it exists.
  2. Step 2: Check each option

    if let unwrappedAge = age { print(unwrappedAge) } correctly unwraps with if let unwrappedAge = age. if age != nil { print(age) } checks nil but does not unwrap. if let age = age? { print(age) } has invalid syntax with age?. if age = nil { print(age) } wrongly assigns nil in condition.
  3. Final Answer:

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

    Safe unwrap = if let variable = optional [OK]
Quick Trick: Use if let variable = optional to unwrap safely [OK]
Common Mistakes:
  • Using assignment (=) instead of comparison (==)
  • Trying to unwrap with question mark in condition
  • Not binding the unwrapped value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes