In this article, we will walk you through the process of setting up GitHub in…
Learn how to work with Xcode Previews in SwiftData App
Learn how to work with Xcode Previews in SwiftData App. SwiftData in combination with SwiftUI allows you to quickly prototype and develop apps by providing easy-to-use, in-memory storage for model data. In this tutorial, weβll walk through how to create sample recipe data using ModelContainer
and display that data in SwiftUI views with previews.
Step 1: Defining the Recipe
Model
Setup a Recipe
model that represents a dish with a name, cooking time, and ingredients.
import Foundation
import SwiftData
@Model
class Recipe {
@Attribute(.unique) var id: UUID
var name: String?
var cookingTime: Int // in minutes
var ingredients: [String]
init(id: UUID = UUID(), name: String? = nil, cookingTime: Int, ingredients: [String]) {
self.id = id
self.name = name
self.cookingTime = cookingTime
self.ingredients = ingredients
}
}
This model defines each recipe with a unique ID, name, cooking time, and a list of ingredients. With this setup, you can now store recipes in SwiftData and use them in your app.
Step 2: Creating Sample Data for Previews
Now, letβs generate sample data using ModelContainer
. Youβve written a method to insert sample recipes into a ModelContainer
for use in previews.
import Foundation
import SwiftData
let sampleRecipes = [
Recipe(name: "Spaghetti Bolognese", cookingTime: 45, ingredients: ["Spaghetti", "Minced Beef", "Tomato Sauce"]),
Recipe(name: "Grilled Cheese Sandwich", cookingTime: 10, ingredients: ["Bread", "Cheese", "Butter"]),
Recipe(name: "Beef Wellington", cookingTime: 120, ingredients: ["Beef Tenderloin", "Puff Pastry", "Mushrooms", "Dijon Mustard"])
]
extension Recipe {
@MainActor static func makeSampleRecipes(in container: ModelContainer) {
let context = container.mainContext
// Check if there are any existing recipes in the database
let existingRecipesCount = (try? context.fetch(FetchDescriptor()))?.count ?? 0
guard existingRecipesCount == 0 else {
// If there are existing recipes, do not create new ones
return
}
// Sample data to create
let recipes = [
Recipe(name: "Spaghetti Bolognese", cookingTime: 45, ingredients: ["Spaghetti", "Minced Beef", "Tomato Sauce"]),
Recipe(name: "Grilled Cheese Sandwich", cookingTime: 10, ingredients: ["Bread", "Cheese", "Butter"]),
Recipe(name: "Beef Wellington", cookingTime: 120, ingredients: ["Beef Tenderloin", "Puff Pastry", "Mushrooms", "Dijon Mustard"])
]
// Insert the sample recipes into the context
for recipe in recipes {
context.insert(recipe)
}
// Save the context to persist the new data
try? context.save()
}
}
This method defines three recipes and inserts them into the ModelContainer
. The @MainActor
annotation ensures this method runs on the main thread, which is essential for interacting with SwiftData.
Step 3: Implementing the RecipeSampleData
for Previews
...π The remaining content of this article is only available on our Substack!
This Post Has 0 Comments