Empty state handling shows a friendly message or image when there is no data to display. It helps users understand what to do next instead of seeing a blank screen.
Empty state handling in iOS Swift
if dataArray.isEmpty { // Show empty state view } else { // Show data list view }
Check if your data source is empty using isEmpty.
Display a special view or message to guide the user when empty.
if items.isEmpty { emptyLabel.isHidden = false } else { emptyLabel.isHidden = true }
tableView.backgroundView = items.isEmpty ? emptyView : nil
guard !data.isEmpty else { showEmptyState() return }
This example shows a table view that displays a list of items. If the list is empty, it hides the table and shows a friendly message label in the center.
import UIKit class EmptyStateViewController: UIViewController, UITableViewDataSource { let tableView = UITableView() let emptyLabel = UILabel() var items: [String] = [] override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white emptyLabel.text = "No items found. Please add some." emptyLabel.textAlignment = .center emptyLabel.textColor = .gray emptyLabel.translatesAutoresizingMaskIntoConstraints = false tableView.dataSource = self tableView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(tableView) view.addSubview(emptyLabel) NSLayoutConstraint.activate([ tableView.topAnchor.constraint(equalTo: view.topAnchor), tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), emptyLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), emptyLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor) ]) updateUI() } func updateUI() { if items.isEmpty { tableView.isHidden = true emptyLabel.isHidden = false } else { tableView.isHidden = false emptyLabel.isHidden = true } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") cell.textLabel?.text = items[indexPath.row] return cell } }
Always provide a clear message or image to help users understand the empty state.
Consider adding a button or action to help users add data or refresh.
Keep empty state UI simple and visually distinct from normal content.
Empty state handling improves user experience by showing helpful messages when no data is available.
Use isEmpty to check data and toggle views accordingly.
Design empty states to guide users on what to do next.
Practice
Solution
Step 1: Understand empty state concept
Empty state means no data is available to show in the UI.Step 2: Purpose of empty state handling
It improves user experience by showing a helpful message or guide instead of a blank screen.Final Answer:
To show a friendly message when there is no data to display -> Option CQuick Check:
Empty state = friendly message [OK]
- Ignoring empty data and showing blank screen
- Crashing app when data is empty
- Reloading data endlessly without message
items is empty?Solution
Step 1: Identify correct Swift property for empty check
Swift arrays have anisEmptyproperty to check if they contain no elements.Step 2: Compare options
if items.isEmpty {} usesitems.isEmpty, which is the recommended and clear way to check emptiness.Final Answer:
if items.isEmpty {} -> Option AQuick Check:
Use isEmpty to check empty array [OK]
- Using count > 0 (checks non-empty)
- Checking items == nil (array is not optional)
- Using length property which does not exist for arrays
items is empty?
let items: [String] = []
if items.isEmpty {
print("No items found")
} else {
print("Items available")
}Solution
Step 1: Check the array content
The arrayitemsis empty as it has no elements.Step 2: Evaluate the if condition
The conditionitems.isEmptyis true, so the first print statement runs.Final Answer:
No items found -> Option AQuick Check:
Empty array triggers "No items found" [OK]
- Assuming else branch runs for empty array
- Expecting no output instead of print
- Thinking code crashes on empty array
var data: [String]?
if data.isEmpty {
print("No data")
}Solution
Step 1: Analyze variable type
Variabledatais an optional array ([String]?), meaning it can be nil.Step 2: Check method call on optional
You cannot callisEmptydirectly on an optional without unwrapping it first.Final Answer:
data is optional, so isEmpty cannot be called directly -> Option BQuick Check:
Optional must be unwrapped before isEmpty [OK]
- Calling isEmpty directly on optional causes compile error
- Ignoring optional and assuming non-optional
- Thinking var vs let affects isEmpty usage
messages array is empty. Which SwiftUI code correctly handles this empty state?Solution
Step 1: Understand the requirement
Show "No messages" label only whenmessagesarray is empty, otherwise show the list.Step 2: Evaluate options
if messages.isEmpty { Text("No messages") } else { List(messages, id: \.self) { Text($0) } } usesif messages.isEmptyto conditionally show the label or the list, which is correct.Step 3: Check other options
if messages.count > 0 { Text("No messages") } else { List(messages, id: \.self) { Text($0) } } reverses the condition incorrectly. Text(messages.isEmpty ? "No messages" : "") shows empty text instead of list. List(messages, id: \.self) { Text($0) } always shows, no empty state never shows empty state.Final Answer:
if messages.isEmpty { Text("No messages") } else { List(messages, id: \.self) { Text($0) } } -> Option DQuick Check:
Use if isEmpty to toggle empty state UI [OK]
- Reversing the condition logic
- Showing empty text instead of list
- Not handling empty state at all
Ready to prove it?
Pass the test and this pattern moves unknown
