Bird
0
0

Given the following SwiftData code snippet, what will be the output?

medium📝 Predict Output Q4 of 15
iOS Swift - Local Data Persistence
Given the following SwiftData code snippet, what will be the output?
let newBook = Book(title: "Swift Basics")
context.insert(newBook)
let books = try context.fetch(Book.fetchRequest())
print(books.count)
A0
BError: fetchRequest not found
C1
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Insert newBook into context

    The code inserts one Book instance into the context, so one object is stored.
  2. Step 2: Fetch all Book objects and count

    Fetching returns all stored Book objects, which includes the one just inserted, so count is 1.
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    Inserted one object, fetch count = 1 [OK]
Quick Trick: Inserted objects appear in fetch count [OK]
Common Mistakes:
  • Assuming fetch returns zero before save
  • Thinking fetchRequest is undefined
  • Expecting nil instead of count

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes