Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to show a label when the data array is empty.
iOS Swift
if data.isEmpty { let emptyLabel = UILabel() emptyLabel.text = [1] view.addSubview(emptyLabel) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a loading or error message instead of an empty state message.
Leaving the label text empty.
✗ Incorrect
The label should show a message indicating no data is available when the array is empty.
2fill in blank
mediumComplete the code to center the empty state label in the view.
iOS Swift
emptyLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
emptyLabel.centerXAnchor.constraint(equalTo: view.[1]),
emptyLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using leadingAnchor or trailingAnchor which align to edges, not center.
Using topAnchor which aligns vertically.
✗ Incorrect
To center horizontally, use centerXAnchor of the view.
3fill in blank
hardFix the error in the code to properly update the UI on the main thread when data is empty.
iOS Swift
DispatchQueue.global().async {
if data.isEmpty {
DispatchQueue.[1].async {
showEmptyState()
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using background or global queues for UI updates causing crashes or no effect.
Forgetting to dispatch UI updates to the main thread.
✗ Incorrect
UI updates must happen on the main thread, so use DispatchQueue.main.
4fill in blank
hardFill both blanks to create a UIView that shows an empty state message with a background color.
iOS Swift
let emptyView = UIView() emptyView.backgroundColor = [1] let messageLabel = UILabel() messageLabel.text = [2] emptyView.addSubview(messageLabel)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using bright or alarming colors like red for empty states.
Using loading messages instead of empty state messages.
✗ Incorrect
A light gray background is common for empty states and the message should inform no items are found.
5fill in blank
hardFill all three blanks to create a function that returns a configured empty state UILabel with accessibility label.
iOS Swift
func createEmptyStateLabel() -> UILabel {
let label = UILabel()
label.text = [1]
label.textColor = [2]
label.accessibilityLabel = [3]
return label
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using black color which is too strong for empty states.
Not setting accessibility label, making app less accessible.
✗ Incorrect
The label text should inform no content, text color gray is subtle, and accessibility label describes the message for screen readers.