Bird
0
0

What will be the result if the search text is empty in this code?

medium📝 Predict Output Q5 of 15
iOS Swift - Lists and Data Display
What will be the result if the search text is empty in this code?
var filteredItems: [String] {
  if searchText.isEmpty { return items }
  return items.filter { $0.hasPrefix(searchText) }
}
AAn empty list is returned
BThe full items list is returned
COnly items starting with empty string are returned
DThe code will crash
Step-by-Step Solution
Solution:
  1. Step 1: Check condition for empty search text

    The code explicitly returns the full items list if searchText.isEmpty is true.
  2. Step 2: Understand fallback behavior

    So when search text is empty, no filtering happens and all items show.
  3. Final Answer:

    The full items list is returned -> Option B
  4. Quick Check:

    Empty search returns full list [OK]
Quick Trick: Empty searchText usually returns full list [OK]
Common Mistakes:
  • Thinking empty string filters to empty list
  • Assuming code crashes on empty string
  • Confusing hasPrefix behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes