0
0
iOS Swiftmobile~20 mins

SwiftData setup (modern persistence) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SwiftData Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the role of the @Model attribute in SwiftData?

In SwiftData, you mark your data model classes with @Model. What does this attribute do?

AIt disables persistence for the class, making it transient only.
BIt marks the class as a UI component to be displayed on screen.
CIt tells SwiftData to manage the class as a persistent data model with automatic storage support.
DIt converts the class into a network data transfer object.
Attempts:
2 left
💡 Hint

Think about what you want SwiftData to do with your data classes.

ui_behavior
intermediate
1:30remaining
What happens if you forget to inject the ModelContext into your SwiftUI view?

You have a SwiftUI view that needs to save data using SwiftData, but you forget to add @Environment(\.modelContext). What will happen when you try to save?

AThe app will crash at runtime with a missing environment key error.
BThe data will save normally without any issues.
CThe data will save but only locally, not persisted.
DThe compiler will show an error and prevent building.
Attempts:
2 left
💡 Hint

Think about how SwiftUI provides the context to views.

lifecycle
advanced
2:00remaining
When is the SwiftData container initialized in a typical SwiftUI app?

In a SwiftUI app using SwiftData, when is the ModelContainer typically created and injected?

AAutomatically by the system without any code needed.
BIn the app's main struct, during initialization, before the first view appears.
CInside each view's <code>onAppear</code> method individually.
DOnly after the user logs in, triggered by a button action.
Attempts:
2 left
💡 Hint

Consider where you set up global app state in SwiftUI.

navigation
advanced
2:00remaining
How do you pass a managed SwiftData model object between views?

You want to navigate from a list view to a detail view showing a SwiftData model object. How should you pass the object?

ACreate a new instance of the model object in the detail view.
BPass only the object's ID and fetch it again in the detail view.
CStore the object in UserDefaults and read it in the detail view.
DPass the model object directly as a parameter to the detail view initializer.
Attempts:
2 left
💡 Hint

Think about how SwiftData manages object identity and sharing.

🔧 Debug
expert
2:30remaining
Why does this SwiftData save operation fail silently?

Consider this code snippet inside a SwiftUI view:

func saveItem() {
  let newItem = Item(name: "Test")
  modelContext.insert(newItem)
  // Missing save call here
}

Why does the new item not persist after app restart?

ABecause <code>modelContext.save()</code> was not called to commit changes.
BBecause the <code>modelContext</code> was not injected into the view.
CBecause the <code>Item</code> model is missing the <code>@Model</code> attribute.
DBecause the item name "Test" is invalid and rejected by SwiftData.
Attempts:
2 left
💡 Hint

Think about what final step is needed to persist changes.