0
0
iOS Swiftmobile~10 mins

Empty state handling in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"Error occurred"
B"Data loaded"
C"Loading..."
D"No data available"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a loading or error message instead of an empty state message.
Leaving the label text empty.
2fill in blank
medium

Complete 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'
AtrailingAnchor
BcenterXAnchor
CleadingAnchor
DtopAnchor
Attempts:
3 left
💡 Hint
Common Mistakes
Using leadingAnchor or trailingAnchor which align to edges, not center.
Using topAnchor which aligns vertically.
3fill in blank
hard

Fix 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'
Amain
Bbackground
Cglobal
Ddefault
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.
4fill in blank
hard

Fill 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'
AUIColor.lightGray
B"No items found"
CUIColor.red
D"Loading data"
Attempts:
3 left
💡 Hint
Common Mistakes
Using bright or alarming colors like red for empty states.
Using loading messages instead of empty state messages.
5fill in blank
hard

Fill 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'
A"No content available"
BUIColor.gray
C"Empty state message"
DUIColor.black
Attempts:
3 left
💡 Hint
Common Mistakes
Using black color which is too strong for empty states.
Not setting accessibility label, making app less accessible.