Bird
0
0

How can you modify this searchable list to show a message "No results found" when the filtered list is empty?

hard📝 Application Q9 of 15
iOS Swift - Lists and Data Display
How can you modify this searchable list to show a message "No results found" when the filtered list is empty?
List(filteredItems, id: \.self) { item in
  Text(item)
}
.searchable(text: $searchText)
AUse a conditional view: if filteredItems.isEmpty { Text("No results found") } else { List(...) }
BAdd .emptyMessage("No results found") modifier to List
CSet searchable prompt to "No results found"
DUse .alert when filteredItems is empty
Step-by-Step Solution
Solution:
  1. Step 1: Understand SwiftUI conditional views

    You can conditionally show different views based on data, such as showing a Text when the list is empty.
  2. Step 2: Implement conditional logic

    Use a conditional view: if filteredItems.isEmpty { Text("No results found") } else { List(...) } correctly uses an if-else to show the message or the list.
  3. Final Answer:

    Use a conditional view: if filteredItems.isEmpty { Text("No results found") } else { List(...) } -> Option A
  4. Quick Check:

    Conditional views show messages for empty lists [OK]
Quick Trick: Use if-else to show message when list is empty [OK]
Common Mistakes:
  • Expecting List to have emptyMessage modifier
  • Misusing searchable prompt for messages
  • Using alert instead of inline message

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes