Bird
0
0

You want to fetch all "Book" entities with more than 100 pages and sort them by title ascending. Which code snippet correctly achieves this?

hard📝 Application Q8 of 15
iOS Swift - Local Data Persistence
You want to fetch all "Book" entities with more than 100 pages and sort them by title ascending. Which code snippet correctly achieves this?
Alet request = NSFetchRequest<Book>(entityName: "Book") request.predicate = NSPredicate(format: "pages > %d", 100) request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]
Blet request = NSFetchRequest<Book>(entityName: "Book") request.predicate = NSPredicate(format: "pages > %@", 100) request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: false)]
Clet request = NSFetchRequest<Book>(entityName: "Book") request.predicate = NSPredicate(format: "pages >= 100") request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]
Dlet request = NSFetchRequest<Book>(entityName: "Book") request.predicate = NSPredicate(format: "pages > 100") request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]
Step-by-Step Solution
Solution:
  1. Step 1: Check predicate correctness

    Use %d with integer 100 for pages > 100 condition.
  2. Step 2: Verify sort descriptor

    Sort by title ascending is correct with ascending: true.
  3. Step 3: Review other options

    let request = NSFetchRequest(entityName: "Book") request.predicate = NSPredicate(format: "pages > %@", 100) request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: false)] uses %@ with Int (wrong), descending sort; C uses >= instead of >; D uses raw number in format string (not recommended).
  4. Final Answer:

    Option A code snippet correctly fetches and sorts -> Option A
  5. Quick Check:

    Use %d for Int and ascending true for sort [OK]
Quick Trick: Use %d for Int in predicate and ascending true for sort [OK]
Common Mistakes:
  • Using %@ with Int in predicate
  • Wrong sort order
  • Using >= instead of >

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes