Bird
0
0

What is wrong with this Swift code for empty state?

medium📝 Debug Q7 of 15
iOS Swift - Lists and Data Display
What is wrong with this Swift code for empty state?
if items.isEmpty {
  Text("No items")
} else {
  List(items, id: \.self) { item in
    Text(item)
  }
}
ANo error, code is correct
BText cannot be used outside a View builder
CList requires a ForEach inside
Ditems.isEmpty is not a valid property
Step-by-Step Solution
Solution:
  1. Step 1: Check if items.isEmpty is valid

    items.isEmpty is a valid property to check if array is empty.
  2. Step 2: Verify usage of Text and List in SwiftUI

    Text and List are valid views used correctly inside the View builder.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Code syntax and logic are correct [OK]
Quick Trick: isEmpty is valid; Text and List are proper views [OK]
Common Mistakes:
  • Thinking isEmpty is invalid
  • Assuming List needs ForEach explicitly
  • Believing Text cannot be used here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes