Bird
0
0

Identify the error in this Swift code for empty state handling:

medium📝 Debug Q14 of 15
iOS Swift - Lists and Data Display
Identify the error in this Swift code for empty state handling:
var data: [String]?
if data.isEmpty {
  print("No data")
}
AUsing var instead of let causes error
Bdata is optional, so isEmpty cannot be called directly
CMissing else block for non-empty data
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable type

    Variable data is an optional array ([String]?), meaning it can be nil.
  2. Step 2: Check method call on optional

    You cannot call isEmpty directly on an optional without unwrapping it first.
  3. Final Answer:

    data is optional, so isEmpty cannot be called directly -> Option B
  4. Quick Check:

    Optional must be unwrapped before isEmpty [OK]
Quick Trick: Optional arrays need unwrapping before isEmpty [OK]
Common Mistakes:
  • Calling isEmpty directly on optional causes compile error
  • Ignoring optional and assuming non-optional
  • Thinking var vs let affects isEmpty usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes