getting an this error, any help?
Please post your code as text rather than a screenshot. That way others can copy/paste your code to work out solutions and won’t have to type everything in from scratch.
Please post your Recipe
model.
okay here’s my Recipe model
import Foundation
class Recipe: Identifiable, Decodable {
var name:String
var featured:Bool
var image: String
var description:String
var prepTime:String
var cookTime:String
var totalTime:String
var servings:Int
var indegredients:[String]
var directions:[String]
}
also here’s the code in the screen shot
import Foundation
class DataService {
static func getLocalData() -> [Recipe] {
// Parse local json file
// Get a url path to the json file
let pathString = Bundle.main.path(forResource: "recipes", ofType: "json")
// Check if pathString is not nil, otherwise...
guard pathString != nil else {
return [Recipe]()
}
// Create a url object
let url = URL(fileURLWithPath: pathString!)
do {
// Create a data object
let data = try Data(contentsOf: url)
// Decode the data with a JSON decoder
let decoder = JSONDecoder()
do {
let recipeData = try decoder.decode([Recipe].self, from: data)
// Add the unique IDs
for r in recipeData {
r.id = UUID()
}
// Return the recipes
return recipeData
}
catch {
// error with parsing json
print(error)
}
}
catch {
// error with getting data
print(error)
}
return [Recipe]()
}
}
Hope you don’t mind me chiming in here Patrick…
Your Recipe model does not have a property named id
so that is why Xcode is yelling at you.
import Foundation
class Recipe: Identifiable, Decodable {
var id: UUID? // <--- Add this property to your Model
var name: String
var featured: Bool
var image: String
var description: String
var prepTime: String
var cookTime: String
var totalTime: String
var servings: Int
var indegredients: [String]
var directions: [String]
}
One question: Is this related to Module 4 of the “iOS Foundations (SwiftUI)” course?
ahh okay thank you so much! didn’t see that. and yes it is referring to module 4 course
There in another property missing in your class too and an error in the definition of ingredients
. If you don’t fix that now it will cause you grief later on when you build the RecipeDetailView
You need to add a property highlights
of type [String] and change ingredients to be of type [Ingredient] and then add the class Ingredient below Recipe.
To save you some time the two classes should be defined like this:
class Recipe: Identifiable, Decodable {
var id: UUID?
var name: String
var featured: Bool
var image: String
var description: String
var prepTime: String
var cookTime: String
var totalTime: String
var servings: Int
var highlights: [String]
var ingredients: [Ingredient]
var directions: [String]
}
class Ingredient: Identifiable, Decodable {
var id: UUID?
var name: String
var num: Int?
var denom: Int?
var unit: String?
}
In your getLocalData() method you will have to adjust your code that assigns the UUID() to the id property in both Recipe and Ingredient like this:
for recipe in recipeData {
recipe.id = UUID()
for ingredient in recipe.ingredients {
ingredient.id = UUID()
}
}