Bird
0
0

What will be the result of this code?

medium📝 Predict Output Q5 of 15
iOS Swift - Local Data Persistence
What will be the result of this code?
let people = ["Anna", "Bob", "Charlie", "David"]
let predicate = NSPredicate(format: "SELF BEGINSWITH[c] 'b'")
let filtered = (people as NSArray).filtered(using: predicate) as! [String]

What does filtered contain?
A["Bob", "bob"]
B["Bob"]
C["bob"]
D[]
Step-by-Step Solution
Solution:
  1. Step 1: Understand predicate condition

    "SELF BEGINSWITH[c] 'b'" means case-insensitive match for strings starting with 'b'.
  2. Step 2: Filter array with predicate

    Only "Bob" matches; "bob" is not in the array.
  3. Final Answer:

    ["Bob"] -> Option B
  4. Quick Check:

    BEGINSWITH[c] filters case-insensitive [OK]
Quick Trick: Use [c] for case-insensitive predicate matching [OK]
Common Mistakes:
  • Assuming case-sensitive match
  • Expecting lowercase 'bob' in result
  • Not casting NSArray back to [String]

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes