Component - Empty state handling
This UI component shows a friendly message and an image when there is no data to display. It helps users understand that the screen is empty and what they can do next.
This UI component shows a friendly message and an image when there is no data to display. It helps users understand that the screen is empty and what they can do next.
UIViewController
├── UIView (main view)
├── UIImageView (empty state image)
└── UILabel (empty state message)items is empty?isEmpty property to check if they contain no elements.items.isEmpty, which is the recommended and clear way to check emptiness.items is empty?
let items: [String] = []
if items.isEmpty {
print("No items found")
} else {
print("Items available")
}items is empty as it has no elements.items.isEmpty is true, so the first print statement runs.var data: [String]?
if data.isEmpty {
print("No data")
}data is an optional array ([String]?), meaning it can be nil.isEmpty directly on an optional without unwrapping it first.messages array is empty. Which SwiftUI code correctly handles this empty state?messages array is empty, otherwise show the list.if messages.isEmpty to conditionally show the label or the list, which is correct.Pass the test and this pattern moves unknown