I am developing "Tasty Recipes" app. I want to load all data related to this app such "Recipe Category", "Recipes List", "Recipes Inscredient", "Preparation Methods" and so on. How can I load and store all data using SwiftData. User is going to use these data. They will update few things like "Favourite Recipes", "Rating". How can I do this.
SwiftData - Load the data into Apps
To use SwiftData you basically need these three things and to read the documentation.
- Add the macro @Model
- Add .modelContainer to the view
- Add the @Environment(.modelContext) private var context
- Finally, in SwiftData you can use FetchDescriptor with a Predicate to fetch and sort.
The following example is in the SwiftData example provided by Apple.
@Model
class Recipe {
@Attribute(.unique) var name: String
var summary: String?
var ingredients: [Ingredient]
}
So, var ingredients
would be an array of elements that are of data type Ingredient. So make that. Example:
@Model
class Ingredients{
var name: String
var amount: String
var somethingElse: ThatType
}
Ingredients
should be Ingredient
@Model
class Ingredient {
var name: String
var amount: String
var somethingElse: ThatType
}