Bird
0
0

Why does NSPredicate(format: "age > %@", 18) sometimes fail silently when filtering an array of dictionaries?

hard📝 Conceptual Q10 of 15
iOS Swift - Local Data Persistence
Why does NSPredicate(format: "age > %@", 18) sometimes fail silently when filtering an array of dictionaries?
ABecause %@ expects an object, but 18 is a primitive Int, causing a type mismatch
BBecause NSPredicate cannot filter dictionaries
CBecause the format string must use %d for numbers
DBecause the key "age" must be a string, not a number
Step-by-Step Solution
Solution:
  1. Step 1: Understand %@ placeholder usage

    %@ expects an object like NSNumber, not a primitive Int.
  2. Step 2: Recognize silent failure cause

    Passing Int directly causes type mismatch, so predicate fails silently.
  3. Final Answer:

    Because %@ expects an object, but 18 is a primitive Int, causing a type mismatch -> Option A
  4. Quick Check:

    Use NSNumber or %d for numeric values in NSPredicate [OK]
Quick Trick: Use NSNumber or %d for numbers in NSPredicate format [OK]
Common Mistakes:
  • Assuming NSPredicate can't filter dictionaries
  • Using wrong format specifiers
  • Thinking key types cause failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes