Bird
0
0

Why does this Core Data fetch fail? let fetchRequest = NSFetchRequest(entityName: "Person") fetchRequest.predicate = NSPredicate(format: "age > %@", 30) let results = try context.fetch(fetchRequest)

medium📝 Debug Q7 of 15
iOS Swift - Local Data Persistence
Why does this Core Data fetch fail? let fetchRequest = NSFetchRequest(entityName: "Person") fetchRequest.predicate = NSPredicate(format: "age > %@", 30) let results = try context.fetch(fetchRequest)
APredicate format string is invalid
BUsing %@ with an Int instead of NSNumber causes predicate failure
CNSFetchRequest generic type should be Person, not NSManagedObject
DEntity name "Person" is misspelled
Step-by-Step Solution
Solution:
  1. Step 1: Understand predicate format specifiers

    %@ expects an object like NSNumber, but 30 is an Int literal, causing failure.
  2. Step 2: Correct usage

    Wrap 30 as NSNumber(value: 30) or use %d for Int in predicate format.
  3. Final Answer:

    Using %@ with an Int instead of NSNumber causes predicate failure -> Option B
  4. Quick Check:

    Use NSNumber with %@ in predicates [OK]
Quick Trick: Use NSNumber for %@ in NSPredicate format [OK]
Common Mistakes:
  • Passing Int directly with %@
  • Assuming generic type causes fetch failure
  • Misreading predicate format string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes