Bird
0
0

Which of the following is the correct syntax to safely access the property count of an optional string name in Swift?

easy📝 Syntax Q3 of 15
Swift - Operators and Expressions
Which of the following is the correct syntax to safely access the property count of an optional string name in Swift?
Alet length = name!.count
Blet length = name?.count
Clet length = name.count?
Dlet length = name??.count
Step-by-Step Solution
Solution:
  1. Step 1: Understand safe property access syntax

    Safe access uses ?. to access properties only if the optional is not nil.
  2. Step 2: Check each option

    name?.count is correct; name!.count force unwraps; name.count? and name??.count are invalid syntax.
  3. Final Answer:

    let length = name?.count -> Option B
  4. Quick Check:

    Safe property access = ?. [OK]
Quick Trick: Use ?. to safely access optional properties [OK]
Common Mistakes:
  • Using ! which can cause crashes
  • Placing ? after property name incorrectly
  • Using double ?? with dot incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes